Skip to content

Commit 6784162

Browse files
authored
Merge pull request #2 from RyougiNevermore/master
update env
2 parents d799cfb + b763404 commit 6784162

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

env.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,47 @@ package errors
33
import (
44
"os"
55
"strings"
6+
"sync"
67
)
78

9+
var gopaths = make([]string, 0, 2)
10+
11+
var _gopathOnce = sync.Once{}
12+
813
func goEnv() []string {
9-
env := make([]string, 0, 2)
10-
// goroot
11-
goroot := os.Getenv("GOROOT")
12-
if len(goroot) > 0 {
13-
if strings.Contains(goroot, `\`) && strings.Contains(goroot, ":") { // win
14-
goroot = strings.Replace(goroot, `\`, "/", -1)
15-
}
16-
env = append(env, goroot)
17-
}
18-
gopath := os.Getenv("GOPATH")
19-
if len(gopath) == 0 {
20-
return env
21-
}
22-
if strings.Contains(gopath, `\`) && strings.Contains(gopath, ":") { // win
23-
gopath = strings.Replace(gopath, `\`, "/", -1)
24-
if strings.Contains(gopath, ";") {
25-
gopaths := strings.Split(gopath, ";")
26-
for _, item := range gopaths {
27-
env = append(env, strings.TrimSpace(item))
14+
_gopathOnce.Do(func() {
15+
// goroot
16+
goroot := os.Getenv("GOROOT")
17+
if len(goroot) > 0 {
18+
if strings.Contains(goroot, `\`) && strings.Contains(goroot, ":") { // win
19+
goroot = strings.Replace(goroot, `\`, "/", -1)
2820
}
29-
} else {
30-
env = append(env, strings.TrimSpace(gopath))
21+
gopaths = append(gopaths, goroot)
3122
}
32-
} else { // unix
33-
if strings.Contains(gopath, ":") {
34-
gopaths := strings.Split(gopath, ":")
35-
for _, item := range gopaths {
36-
env = append(env, strings.TrimSpace(item))
23+
gopath := os.Getenv("GOPATH")
24+
if len(gopath) == 0 {
25+
return
26+
}
27+
if strings.Contains(gopath, `\`) && strings.Contains(gopath, ":") { // win
28+
gopath = strings.Replace(gopath, `\`, "/", -1)
29+
if strings.Contains(gopath, ";") {
30+
gopaths := strings.Split(gopath, ";")
31+
for _, item := range gopaths {
32+
gopaths = append(gopaths, strings.TrimSpace(item))
33+
}
34+
} else {
35+
gopaths = append(gopaths, strings.TrimSpace(gopath))
36+
}
37+
} else { // unix
38+
if strings.Contains(gopath, ":") {
39+
gopaths := strings.Split(gopath, ":")
40+
for _, item := range gopaths {
41+
gopaths = append(gopaths, strings.TrimSpace(item))
42+
}
43+
} else {
44+
gopaths = append(gopaths, strings.TrimSpace(gopath))
3745
}
38-
} else {
39-
env = append(env, strings.TrimSpace(gopath))
4046
}
41-
}
42-
return env
47+
})
48+
return gopaths
4349
}

0 commit comments

Comments
 (0)