$GOPATH must not be set to $GOROOT, why not?

Add following lines to your .bashrc file:

export GOPATH="${HOME}/workspace"
export GOROOT="${HOME}/go"
export PATH="${GOPATH}/bin:${PATH}"

Then load the ~/.bashrc file: $ source .bashrc


Because /usr/local/go/src already contains the code for the standard library, and you should keep your own code separate from that.

I know, other development tools would have no problem with that, but Go is a little more strict in some ways. It's probably the same philosophy that lies behind flagging unused variables or imports as errors - avoiding problems which may seem small at first, but can lead to bigger headaches in the future.


From go help gopath:

GOPATH must be set to get, build and install packages outside the standard Go tree.

Thus, GOROOT sets the location of standard library, and GOPATH is for nonstandard libraries. One library should not be at both locations at once.


This directory may be where you $GOROOT is, but you can always check via go env, and this will list GOROOT as one of the environment variables:

$ go env 
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.2.1/libexec" # <- its right here
GOTOOLDIR="/usr/local/Cellar/go/1.2.1/libexec/pkg/tool/darwin_amd64"
TERM="dumb"
CC="clang"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common"
CXX="clang++"
CGO_ENABLED="1"

So, your installation might have it in a different place, but either way you cannot make the same path for both GOROOT and GOPATH, the reason being is that your GOPATH also has a src folder, which houses the standard library:

@ /usr/local/Cellar/go/1.2.1/libexec/src/pkg
± % ls                                                                    
archive   container errors    hash      log       path      strconv   text
bufio     crypto    expvar    html      math      reflect   strings   time
builtin   database  flag      image     mime      regexp    sync      unicode
bytes     debug     fmt       index     net       runtime   syscall   unsafe

The reason why you can't have them in the same place is because when you run go install it will look in both GOROOT and GOPATH, and it will see that all your imports like fmt and os are present in both GOROOT and GOPATH, and thus the poor go compiler will be confused and start shouting at you (which it did in your case).

Tags:

Go