-
Notifications
You must be signed in to change notification settings - Fork 196
/
util.go
48 lines (41 loc) · 937 Bytes
/
util.go
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
42
43
44
45
46
47
48
package util
import (
"bufio"
"io"
"os"
"path/filepath"
"runtime"
)
// 检查文件或目录是否存在
// 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false
func Exist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
func ReadFileLines(filename string) (lines []string, err error) {
file, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
return
}
defer file.Close()
bio := bufio.NewReader(file)
for {
var line []byte
line, _, err = bio.ReadLine()
if err != nil {
if err == io.EOF {
file.Close()
return lines, nil
}
return
}
lines = append(lines, string(line))
}
}
func CurrentDir(path ...string) string {
_, currentFilePath, _, _ := runtime.Caller(1)
if len(path) == 0 {
return filepath.Dir(currentFilePath)
}
return filepath.Join(filepath.Dir(currentFilePath), filepath.Join(path...))
}