Merge pull request #45 from Deleplace/default-gopath

Handle default gopath $HOME/go when env var $GOPATH is not set.
This commit is contained in:
Eyal Posener 2017-07-30 22:30:24 +03:00 committed by GitHub
commit f4461a52b6
1 changed files with 19 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"go/build" "go/build"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"strings" "strings"
@ -71,7 +72,7 @@ func listPackages(dir string) (directories []string) {
func systemDirs(dir string) (directories []string) { func systemDirs(dir string) (directories []string) {
// get all paths from GOPATH environment variable and use their src directory // get all paths from GOPATH environment variable and use their src directory
paths := strings.Split(os.Getenv("GOPATH"), ":") paths := findGopath()
for i := range paths { for i := range paths {
paths[i] = filepath.Join(paths[i], "src") paths[i] = filepath.Join(paths[i], "src")
} }
@ -106,3 +107,20 @@ func systemDirs(dir string) (directories []string) {
} }
return return
} }
func findGopath() []string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
// By convention
// See rationale at https://github.com/golang/go/issues/17262
usr, err := user.Current()
if err != nil {
return nil
}
usrgo := filepath.Join(usr.HomeDir, "go")
return []string{usrgo}
}
listsep := string([]byte{os.PathListSeparator})
entries := strings.Split(gopath, listsep)
return entries
}