summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeleplace <[email protected]>2017-07-30 19:41:18 +0200
committerDeleplace <[email protected]>2017-07-30 19:41:18 +0200
commit4bd1bbca4a8d2970a788eaaffae648cc03a2b9eb (patch)
tree80d385ae208b10bd2c843b2f15b81e471851bb72
parentc111db7af1861723f8143fac03496f0acf06295a (diff)
Handle default gopath $HOME/go when env var $GOPATH is not set.
-rw-r--r--gocomplete/pkgs.go20
1 files changed, 19 insertions, 1 deletions
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
+}