cannot download, $GOPATH not set

[Update: as of Go 1.8, GOPATH defaults to $HOME/go, but you may still find this useful if you want to understand the GOPATH layout, customize it, etc.]

The official Go site discusses GOPATH and how to lay out a workspace directory.

export GOPATH="$HOME/your-workspace-dir/" — run it in your shell, then add it to ~/.bashrc or equivalent so it will be set for you in the future. Go will install packages under src/, bin/, and pkg/, subdirectories there. You’ll want to put your own packages somewhere under $GOPATH/src, like $GOPATH/src/github.com/myusername/ if you want to publish to GitHub. You’ll also probably want export PATH=$PATH:$GOPATH/bin in your .bashrc so you can run compiled programs under $GOPATH.

Optionally, via Rob Pike, you can also set CDPATH so it’s faster to cd to package dirs in bash: export CDPATH=.:$GOPATH/src/github.com:$GOPATH/src/golang.org/x means you can just type cd net/html instead of cd $GOPATH/src/golang.org/x/net/html.

Keith Rarick notes you can set GOPATH=$HOME to put Go’s src/, pkg/ and bin/ directories right under your homedir. That can be nice (for instance, you might already have $HOME/bin in your path) but of course some folks use multiple workspaces, etc.

Leave a Comment