-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
58 lines (52 loc) · 968 Bytes
/
utils.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
49
50
51
52
53
54
55
56
57
58
package rod_helper
import (
"os"
"path/filepath"
)
// IsDir 存在且是文件夹
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// IsFile 存在且是文件
func IsFile(filePath string) bool {
s, err := os.Stat(filePath)
if err != nil {
return false
}
return !s.IsDir()
}
// WriteFile 写文件
func WriteFile(desFileFPath string, bytes []byte) error {
var err error
nowDesPath := desFileFPath
if filepath.IsAbs(nowDesPath) == false {
nowDesPath, err = filepath.Abs(nowDesPath)
if err != nil {
return err
}
}
// 创建对应的目录
nowDirPath := filepath.Dir(nowDesPath)
if IsDir(nowDirPath) == false {
err = os.MkdirAll(nowDirPath, os.ModePerm)
if err != nil {
return err
}
}
file, err := os.Create(nowDesPath)
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
_, err = file.Write(bytes)
if err != nil {
return err
}
return nil
}