-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
58 lines (52 loc) · 1.06 KB
/
util.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 main
import (
"golang.org/x/tools/go/packages"
"log"
"os"
"path"
"strings"
)
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}
func filename(file string) (string, string) {
dir, file := path.Split(file)
return dir, file
}
func firstLower(str string) string {
return strings.ToLower(str[:1]) + str[1:]
}
func pkgPath(str string) string {
load, err := packages.Load(&packages.Config{
Dir: str,
Mode: packages.NeedModule,
})
if err != nil {
panic(err)
}
return load[0].ID
}
func snakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
// or通过ASCII码进行大小写的转化
// 65-90(A-Z),97-122(a-z)
//判断如果字母为大写的A-Z就在前面拼接一个_
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
//ToLower把大写字母统一转小写
return strings.ToLower(string(data[:]))
}