From 4bd1bbca4a8d2970a788eaaffae648cc03a2b9eb Mon Sep 17 00:00:00 2001 From: Deleplace Date: Sun, 30 Jul 2017 19:41:18 +0200 Subject: [PATCH] Handle default gopath $HOME/go when env var $GOPATH is not set. --- gocomplete/pkgs.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gocomplete/pkgs.go b/gocomplete/pkgs.go index 8110ff9..2f95046 100644 --- a/gocomplete/pkgs.go +++ b/gocomplete/pkgs.go @@ -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 +}