Handle default gopath $HOME/go when env var $GOPATH is not set.

This commit is contained in:
Deleplace 2017-07-30 19:41:18 +02:00
parent c111db7af1
commit 4bd1bbca4a
1 changed files with 19 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"go/build"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
@ -71,7 +72,7 @@ func listPackages(dir string) (directories []string) {
func systemDirs(dir string) (directories []string) {
// get all paths from GOPATH environment variable and use their src directory
paths := strings.Split(os.Getenv("GOPATH"), ":")
paths := findGopath()
for i := range paths {
paths[i] = filepath.Join(paths[i], "src")
}
@ -106,3 +107,20 @@ func systemDirs(dir string) (directories []string) {
}
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
}