-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinputArgs.go
More file actions
41 lines (38 loc) · 753 Bytes
/
inputArgs.go
File metadata and controls
41 lines (38 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package go_utils
import (
"bufio"
"io"
"log"
"os"
"strings"
)
func DoReadInputStream(r io.Reader, out chan *string) {
buf := bufio.NewScanner(r)
for buf.Scan() {
if s := strings.TrimSpace(buf.Text()); s != "" {
out <- &s
}
}
}
// 获取简单命令行输入
func GetSimpleInput() <-chan *string {
var out = make(chan *string)
go func() {
defer close(out)
if 1 < len(os.Args) {
if FileExists(os.Args[1]) {
if f, err := os.OpenFile(os.Args[1], os.O_RDONLY, os.ModePerm); nil == err {
defer f.Close() //nolint
DoReadInputStream(f, out)
} else {
log.Println("os.OpenFile ", os.Args[1], err)
}
} else {
out <- &os.Args[1]
}
} else {
DoReadInputStream(os.Stdin, out)
}
}()
return out
}