-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
49 lines (41 loc) · 921 Bytes
/
local.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
package main
import (
"os"
"path/filepath"
"runtime"
"strings"
)
var (
gGoPath string
)
func GetGopath() string {
if gGoPath == "" {
s := os.Getenv("GOPATH")
if ss := strings.Split(s, ";"); ss != nil && len(ss) > 0 {
gGoPath = formatPath(ss[0]) + "/src"
}
}
return gGoPath
}
func formatPath(s string) string {
return filepath.ToSlash(filepath.Clean(s))
}
func GetProjectRootpath(depth int) string {
return formatPath(filepath.Dir(GetThisFilepath(depth)))
}
func GetThisFilepath(depth int) string {
thisFile, _ := FileLine(depth)
thisFilePath := filepath.Dir(thisFile)
return formatPath(thisFilePath)
}
func GetReleatGopath(s string) string {
ss, _ := filepath.Rel(GetGopath(), s)
return formatPath(ss)
}
//the caller's file/line info
func FileLine(depth int) (file string, line int) {
if _, __file, __line, __ok := runtime.Caller(depth); __ok {
file, line = __file, __line
}
return
}