utils.go 569 B

123456789101112131415161718192021222324252627282930
  1. package utils
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. "strings"
  7. )
  8. // GetGOPATHs returns all paths in GOPATH variable.
  9. func GetGOPATHs() []string {
  10. gopath := os.Getenv("GOPATH")
  11. if gopath == "" && strings.Compare(runtime.Version(), "go1.8") >= 0 {
  12. gopath = defaultGOPATH()
  13. }
  14. return filepath.SplitList(gopath)
  15. }
  16. func defaultGOPATH() string {
  17. env := "HOME"
  18. if runtime.GOOS == "windows" {
  19. env = "USERPROFILE"
  20. } else if runtime.GOOS == "plan9" {
  21. env = "home"
  22. }
  23. if home := os.Getenv(env); home != "" {
  24. return filepath.Join(home, "go")
  25. }
  26. return ""
  27. }