Skip to content

Instantly share code, notes, and snippets.

@jbuberel
Last active September 3, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbuberel/f84aca6d802b21945b57 to your computer and use it in GitHub Desktop.
Save jbuberel/f84aca6d802b21945b57 to your computer and use it in GitHub Desktop.

Background

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.

Proposal

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!

Existing Implementations

This is nearly identical to what it implemented in the gb tool today. See the about file for how this is communicated to users.

Considerations and Pitfalls

User who forgets to set $GOPATH

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.

Compatibility with the go1.5 vendor experiment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment