@@ -18,15 +18,22 @@ import (
1818 "errors"
1919 "os"
2020 "path/filepath"
21- "runtime"
2221 "strings"
2322)
2423
2524// GetGOPATHs returns all paths in GOPATH variable.
2625func GetGOPATHs () []string {
2726 gopath := os .Getenv ("GOPATH" )
27+ if len (gopath ) == 0 {
28+ if home , err := HomeDir (); err == nil {
29+ userGopath := filepath .Join (home , `go` )
30+ if fi , err := os .Stat (userGopath ); err == nil && fi .IsDir () {
31+ gopath = userGopath
32+ }
33+ }
34+ }
2835 var paths []string
29- if runtime . GOOS == "windows" {
36+ if IsWindows {
3037 gopath = strings .Replace (gopath , "\\ " , "/" , - 1 )
3138 paths = strings .Split (gopath , ";" )
3239 } else {
@@ -36,12 +43,14 @@ func GetGOPATHs() []string {
3643}
3744
3845func FixDirSeparator (fpath string ) string {
39- if runtime . GOOS == "windows" {
46+ if IsWindows {
4047 fpath = strings .Replace (fpath , "\\ " , "/" , - 1 )
4148 }
4249 return fpath
4350}
4451
52+ var ErrFolderNotFound = errors .New ("unable to locate source folder path" )
53+
4554// GetSrcPath returns app. source code path.
4655// It only works when you have src. folder in GOPATH,
4756// it returns error not able to locate source folder path.
@@ -55,11 +64,11 @@ func GetSrcPath(importPath string) (appPath string, err error) {
5564 }
5665
5766 if len (appPath ) == 0 {
58- return "" , errors . New ( "Unable to locate source folder path" )
67+ return "" , ErrFolderNotFound
5968 }
6069
6170 appPath = filepath .Dir (appPath ) + "/"
62- if runtime . GOOS == "windows" {
71+ if IsWindows {
6372 // Replace all '\' to '/'.
6473 appPath = strings .Replace (appPath , "\\ " , "/" , - 1 )
6574 }
@@ -69,19 +78,6 @@ func GetSrcPath(importPath string) (appPath string, err error) {
6978
7079// HomeDir returns path of '~'(in Linux) on Windows,
7180// it returns error when the variable does not exist.
72- func HomeDir () (home string , err error ) {
73- if runtime .GOOS == "windows" {
74- home = os .Getenv ("HOMEDRIVE" ) + os .Getenv ("HOMEPATH" )
75- if home == "" {
76- home = os .Getenv ("USERPROFILE" )
77- }
78- } else {
79- home = os .Getenv ("HOME" )
80- }
81-
82- if len (home ) == 0 {
83- return "" , errors .New ("Cannot specify home directory because it's empty" )
84- }
85-
86- return home , nil
81+ func HomeDir () (string , error ) {
82+ return os .UserHomeDir ()
8783}
0 commit comments