Skip to content

Instantly share code, notes, and snippets.

@vsouza
Last active September 21, 2024 12:31
Show Gist options
  • Save vsouza/77e6b20520d07652ed7d to your computer and use it in GitHub Desktop.
Save vsouza/77e6b20520d07652ed7d to your computer and use it in GitHub Desktop.
Golang setup in Mac OSX with HomeBrew. Set `GOPATH` and `GOROOT` variables in zshell, fish or bash.
# Set variables in .bashrc file
# don't forget to change your path correctly!
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
# Set variables in .zshrc file
# don't forget to set path correctly!
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
# Set variables in config.fish file
# don't forget to set path correctly!
# GOLANG configurations
set -x GOPATH $HOME/golang
set -x GOROOT /usr/local/opt/go/libexec
set PATH $GOPATH/bin $GOROOT/bin $PATH
echo "Please enter your golang path (ex: $HOME/golang) :"
read gopath
echo "Please enter your github username (ex: vsouza) :"
read user
mkdir $gopath
mkdir -p $gopath/src/github.com/$user
export GOPATH=$gopath
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew install go
brew install git
go get golang.org/x/tools/cmd/godoc
go get golang.org/x/tools/cmd/vet
@a0s
Copy link

a0s commented Sep 13, 2016

BTW, we should set only $GOPATH and $PATH (for brew at least), $GOROOT integrated inside go's binary.

# cat ~/.bashrc
export GOPATH=$HOME/golang
export PATH=$PATH:/usr/local/opt/go/libexec/bin
# ls -all /usr/local/opt/go
lrwxr-xr-x  1 orangeudav  admin  18 12 сен 21:57 /usr/local/opt/go -> ../Cellar/go/1.7.1
# go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/orangeudav/golang"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.1/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g8/551vgzq10c7bdq3k7zby5_gw0000gn/T/go-build818624631=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

@kathgironpe
Copy link

Found that you don't need to install godoc

@Gerst20051
Copy link

Why is brew install mercurial installed?

@vsouza
Copy link
Author

vsouza commented May 28, 2019

@Gerst20051 Was the old golang setup, Isn't necessary anymore.

@Shosta
Copy link

Shosta commented Jun 13, 2019

Thanks a lot for that script.
It can be updated and improved slightly but it is highly required. :-D
I can't understand why there isn't anything like that from the Golang website?
Either MacOS Package and an install script or via Brew and an install script.

Thanks a lot for your work. That was really helpful.

@syafilm
Copy link

syafilm commented Oct 20, 2019

thanks a lot this gist save my life lol

@love2d-lua
Copy link

BTW, we should set only $GOPATH and $PATH (for brew at least), $GOROOT integrated inside go's binary.

# cat ~/.bashrc
export GOPATH=$HOME/golang
export PATH=$PATH:/usr/local/opt/go/libexec/bin
# ls -all /usr/local/opt/go
lrwxr-xr-x  1 orangeudav  admin  18 12 сен 21:57 /usr/local/opt/go -> ../Cellar/go/1.7.1
# go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/orangeudav/golang"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.1/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g8/551vgzq10c7bdq3k7zby5_gw0000gn/T/go-build818624631=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

thanks!

@yyong37
Copy link

yyong37 commented Jun 10, 2020

@dagadbm good

@rainb3rry
Copy link

Big thank you

@Markvandersteen
Copy link

Thanks!

@forliuyifei
Copy link

Setup for fish users

# GOLANG configurations
set -x GOPATH $HOME/golang
set -x GOROOT /usr/local/opt/go/libexec
set PATH $GOPATH/bin $GOROOT/bin $PATH

thanks bro

@cedricloneux
Copy link

worked for me by changing
export GOPATH=$HOME/golang into export GOPATH=$HOME/go

@javaducky
Copy link

javaducky commented Feb 7, 2022

Anyone on Apple silicon will need to instead use the updated prefix for Homebrew, i.e. /opt/homebrew instead of /usr/local. The remainder of the paths will be the same.

The .zshrc example would therefore be:

# Set variables in .zshrc file

# don't forget to set path correctly!

export GOPATH=$HOME/golang
export GOROOT=/opt/homebrew/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

@chenzxcvb
Copy link

chenzxcvb commented Feb 13, 2022

@javaducky OMG, you saved my life!!!

/opt/homebrew instead of /usr/local for M1

I wish I read this at the beginning 😭

@0xlxy
Copy link

0xlxy commented Nov 9, 2022

@javaducky thanks man - super helpful!

@mteodori
Copy link

better get the prefix via brew --prefix

@cbstodd
Copy link

cbstodd commented Jan 25, 2024

Anyone on Apple silicon will need to instead use the updated prefix for Homebrew, i.e. /opt/homebrew instead of /usr/local. The remainder of the paths will be the same.

The .zshrc example would therefore be:

# Set variables in .zshrc file

# don't forget to set path correctly!

export GOPATH=$HOME/golang
export GOROOT=/opt/homebrew/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

👍🏼

@Lewiscowles1986
Copy link

yeah I used go env first, and then used this as a guide to modify my .zshrc, trusting that go env was correct. Annoyingly GOBIN was '' though, so I'm not 100% sure the stability of homebrew golang recipe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment