/
file.go
50 lines (44 loc) · 1.04 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
package gokits
import (
"os"
"os/exec"
"path/filepath"
)
// checks whether a file or directory exists.
// It returns false when the file or directory does not exist.
func FileExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// returns true if given path is a file,
// or returns false when it's a directory or does not exist.
func IsFile(filePath string) bool {
f, e := os.Stat(filePath)
if e != nil {
return false
}
return !f.IsDir()
}
// returns true if given path is a directory,
// or returns false when it's a file or does not exist.
func IsDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}
// returns file size in bytes and possible error.
func FileSize(file string) (int64, error) {
f, err := os.Stat(file)
if err != nil {
return 0, err
}
return f.Size(), nil
}
// returns the complete directory of the current execution file
func GetProcPwd() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(filepath.Dir(file))
return path
}