gogopath

What should be the values of GOPATH and GOROOT?


I'm trying to install doozer like this:

$ goinstall github.com/ha/doozer

I get these errors.

goinstall: os: go/build: package could not be found locally
goinstall: fmt: go/build: package could not be found locally
goinstall: io: go/build: package could not be found locally
goinstall: reflect: go/build: package could not be found locally
goinstall: math: go/build: package could not be found locally
goinstall: rand: go/build: package could not be found locally
goinstall: url: go/build: package could not be found locally
goinstall: net: go/build: package could not be found locally
goinstall: sync: go/build: package could not be found locally
goinstall: runtime: go/build: package could not be found locally
goinstall: strings: go/build: package could not be found locally
goinstall: sort: go/build: package could not be found locally
goinstall: strconv: go/build: package could not be found locally
goinstall: bytes: go/build: package could not be found locally
goinstall: log: go/build: package could not be found locally
goinstall: encoding/binary: go/build: package could not be found locally

Solution

  • GOPATH is discussed in the cmd/go documentation:

    The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.

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

    Set GOPATH to a custom directory for installing downloaded packages.

    GOROOT is discussed in the installation instructions:

    The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed.

    For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

    export GOROOT=$HOME/go
    export PATH=$PATH:$GOROOT/bin
    

    Note: GOROOT must be set only when installing to a custom location.

    As of CY2023, for any modern Go installation, do not set/export GOROOT.

    (updated version of Chris Bunch's answer.)