-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpwdtools.go
48 lines (43 loc) · 1023 Bytes
/
pwdtools.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 pwdtools
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/ThreeKing2018/goutil/logtool"
)
//获取当前执行文件的目录
func GetCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
//返回绝对路径
filepath.Dir(os.Args[0])
if err != nil {
logtool.Fetal(err.Error())
}
return strings.Replace(dir, "\\", "/", -1) //将\替换成/
}
//获取执行文件当前的目录 最后带/
func GetRootDir() string {
// 文件不存在获取执行路径
file, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
file = fmt.Sprintf(".%s", string(os.PathSeparator))
} else {
file = fmt.Sprintf("%s%s", file, string(os.PathSeparator))
}
return file
}
//获取执行文件的路径
func GetExecFilePath() string {
file, err := exec.LookPath(os.Args[0])
if err != nil {
file = fmt.Sprintf(".%s", string(os.PathSeparator))
} else {
file, err = filepath.Abs(file)
if err != nil {
logtool.Fetal(err.Error())
}
}
return file
}