-
Notifications
You must be signed in to change notification settings - Fork 6
/
package.go
43 lines (35 loc) · 1009 Bytes
/
package.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
package gox
import (
"strings"
)
// Package 包名
type Package string
func (p *Package) String() string {
return p.Name()
}
func (p *Package) Name() (name string) {
domain := string(*p)
// 包名不能包含特殊字符,比如-、_、@、#等
domain = strings.ReplaceAll(domain, "-", ".")
domain = strings.ReplaceAll(domain, "_", ".")
domain = strings.ReplaceAll(domain, "@", ".")
domain = strings.ReplaceAll(domain, "#", ".")
originalHosts := strings.Split(domain, ".")
hosts := make([]string, 0, len(originalHosts))
for _, originalHost := range originalHosts {
host := strings.TrimSpace(originalHost)
if "" != host {
hosts = append(hosts, host)
}
}
// 包名必须包含多个单词,如果只有一个单词,默认增加一个com后缀
if 0 == len(hosts) {
hosts = append(hosts, "com")
}
// 反转,和包名规范一致
for i, j := 0, len(hosts)-1; i < j; i, j = i+1, j-1 {
hosts[i], hosts[j] = hosts[j], hosts[i]
}
name = strings.Join(hosts, ".")
return
}