Cannot set $GOPATH on Mac OSX

The accepted answer didn't work for me. I investigated and found the cause: I am using zsh, not bash.

I need to add the following two lines to ~/.zshrc:

export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH

Update, as of Go 1.8: If you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you.

It defaults to $HOME/go on macOS (nee OS X) - e.g. /Users/matt/go/. This makes getting started with Go even easier, and you can go get <package> right after installing Go.


For the shell: (the manual method)

~/.bash_profile should contain export GOPATH=$HOME/go and also export PATH=$GOPATH/bin:$PATH. The use of the $ is important: make sure to note where I've used it (and where I have not).

For Sublime Text:

Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User

{
        "shell": ["/bin/bash"],
        "env": {"GOPATH": "/Users/#USERNAME#/go/"},
}

Make sure your GOPATH is not set to the full path of the package; just the root of your go folder where src, pkg, and bin reside. If you're not using GoSublime, I'd suggest installing that first.


You don't put the $ prefix on a variable when you're assigning it, only when you're reading it.

export GOPATH=$HOME

To make this permanent, put the command in your .bash_profile.

That will work for Terminal shells. If you need to set environment variables that will affect GUI applications, see Environment variables in Mac OS X