-
Notifications
You must be signed in to change notification settings - Fork 11
/
file.go
77 lines (67 loc) · 1.36 KB
/
file.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package utils
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var File = NewFile()
func NewFile() *file {
return &file{}
}
type file struct {
}
// file or path is exists
func (f *file) PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// read file data
func (f *file) ReadAll(path string) (data string, err error) {
fi, err := os.Open(path)
if err != nil {
return
}
defer fi.Close()
fd, err := ioutil.ReadAll(fi)
return string(fd), nil
}
// write file
func (f *file) WriteFile(filename string, data string) (err error) {
var dataByte = []byte(data)
err = ioutil.WriteFile(filename, dataByte, 0666)
if err != nil {
return
}
return
}
// create file
func (f *file) CreateFile(filename string) error {
newFile, err := os.Create(filename)
defer newFile.Close()
return err
}
// get dir all files
func (f *file) WalkDir(dirPth, suffix string) (files []string, err error) {
files = make([]string, 0, 30)
suffix = strings.ToUpper(suffix)
err = filepath.Walk(dirPth, func(filename string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
files = append(files, filename)
}
return nil
})
return files, err
}