Setting Go Environment Variables

December 7, 2019

Every time I install Go on to my new machine, I forget to do something that eventually bites me. Setting my GOPATH/GOBIN variables. If they are not set, whenever you use go get to install binaries, the commands will not be found.

The easy steps to fix this (which I always forget are):

  1. Setting the GOPATH variable if it’s not set. There are 2 methods.
    Running go env -w GOPATH=$HOME/go where $HOME/go or adding export GOPATH=$HOME/go to your rc file (in most cases .bashrc or .zshrc.
  2. Setting the GOBIN variable if it’s not set. There are again 2 methods.
    Running go env -w GOBIN=$HOME/go/bin, where $HOME/go/bin has binaries obtained through go get. You can also add the following lines to your rc file, export $GOBIN=$HOME/go/bin.
  3. Finally just add the GOBIN variable to your path by adding export PATH=$PATH:$GOBIN to your rc file.