In conversations with several community members who have been in the position of teaching new Go programmers how the Go toolchain works, $GOPATH
is often
mentioned as an unecessary hurdle. For some users, especially on Windows,
setting environment variables is not second nature.
When users are attempting to run their first Go programs, supporting a default value for $GOPATH
would make life simpler.
Modify the go tool to support a default $GOPATH
value when the actual environment variable is unset.
Starting in the current directory where the go tool is invoked:
- Walk the directory tree above the current path.
- If a directory named 'src' is found, use the parent directory of that directory as the
$GOPATH
value. - If a directory named 'src' is not found, use the current directory as the
$GOPATH
value.
Example 1: Inside a subdirectory that has a src
directory in the containing path:
$ tree nogopath/
nogopath/
└── src
└── github.com
└── jbuberel
└── foo
$ cd nogopath/src/github.com/jbuberel/
$ pwd
/Users/jbuberel/nogopath/src/github.com/jbuberel
$ go env
[snip]
GOPATH="/Users/jbuberel/nogopath"
[snip]
$ go run foo/prog.go
Hello, world!
Example 2: Inside a directory tree that has no src
dir in the containing path:
$ tree nogopath/
nogopath/
└── gosrc
└── github.com
└── jbuberel
└── foo
$ cd nogopath/gosrc/github.com/jbuberel/
$ pwd
/Users/jbuberel/nogopath/gosrc/github.com/jbuberel
$ go env
[snip]
GOPATH="/Users/jbuberel/nogopath/gosrc/github.com/jbuberel"
[snip]
$ go run foo/prog.go
Hello, world!
This is nearly identical to what it implemented in the gb
tool today. See the about file for how this is communicated to users.
Consider a user who is working on a large Go project that has vendored third party dependencies in /usr/local/third_party/go/src/...
and the main project files are located in /home/jbuberel/goproj/src/...
.
When the user attempts to build their project, they'll receieve an error message indicating that the external packages could not be found. That error message currently includes the list of directories that were considered. That should be enough information for users to identify the source of the problem.