GOPATH problems (in non-default Shells)

This caused me some headaches, so I’m putting it here to solve anyone else’s.

It turns out that non-default Shells don’t use the recommended GOPATH setting, so if you’re using Zsh, FISH or others, you’ll need to set GOPATH in /etc/paths (not your .bash_profile). I’d been having trouble installing Go on my Mac (without Homebrew) and also building Hugo from source, so hopefully this will solve these issues also (so I’m not pestering @natefinch to build and send me dev versions).

Unless you either use the global path in /etc/paths or set the $PATH in .zshrc or config.fish the $PATH will self-erase the next time you open up a new terminal window/tab.

https://github.com/Homebrew/homebrew/pull/31288

Here’s how I do it in my zshenv file for what its worth, with an OS specific wrapper since I use the same zshenv file on linux too:

if [[ "$OSTYPE" == darwin* ]]; then
  # Add gopath
  export GOPATH=~/Admin/go
  path=(
    /usr/local/go/bin
    $path
  )
path=(
    /opt/local/bin:/opt/local/sbin
    $path
  )
fi

You’d need to correct the actual path to match you’re own system of course.

1 Like