Skip to content

Instantly share code, notes, and snippets.

@vsouza vsouza/.bashrc
Last active Feb 22, 2020

Embed
What would you like to do?
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
@yaauie

This comment has been minimized.

Copy link

yaauie commented Jul 2, 2015

This was helpful for me on Mac OSX Yosemite (10.10.4); a couple notes:

  • $HOME gets doubled when creating the path for your own code, probably accidentally.
  • brew update && brew upgrade is a little aggressive, installing updates to everything.
  • code.google.com no longer hosts the go tools; replace code.google.com/p/go.tools with golang.org/x/tools
@vsouza

This comment has been minimized.

Copy link
Owner Author

vsouza commented Jul 4, 2015

thanks @yaauie 😄

@danieledangeli

This comment has been minimized.

Copy link

danieledangeli commented Jul 17, 2015

Hey @vsouza I think you should update install.sh with this changes:

go get golang.org/x/tools/cmd/godod
go get golang.org/x/tools/cmd/vet

Because they have been moved:

"can't load package: the code.google.com/p/go.tools/cmd/vet command has moved; use golang.org/x/tools/cmd/vet instead."

@pdxdan

This comment has been minimized.

Copy link

pdxdan commented Jul 20, 2015

@danieledangeli I think there's a typo there where you meant godoc, not godod

@cihann

This comment has been minimized.

Copy link

cihann commented Aug 9, 2015

i got an error. maybe you can update your gist.

go get code.google.com/p/go.tools/cmd/godoc
the code.google.com/p/go.tools/cmd/godoc command has moved; use golang.org/x/tools/cmd/godoc instead.

@vsouza

This comment has been minimized.

Copy link
Owner Author

vsouza commented Sep 14, 2015

Bug fixes and links updated. Thank you all. ❤️

@orip

This comment has been minimized.

Copy link

orip commented Dec 6, 2015

Suggestion to make .zshrc more zsh-y:

path=(
  $path
  $GOPATH/bin
  $GOROOT/bin
)
@ikrauchanka

This comment has been minimized.

Copy link

ikrauchanka commented Dec 14, 2015

thanks for bashrc

@rawaludin

This comment has been minimized.

Copy link

rawaludin commented Dec 19, 2015

Thanks man.. :)

@Anonyfox

This comment has been minimized.

Copy link

Anonyfox commented Dec 30, 2015

Goroot -> the "/libexec" part does the trick for me on OSX - thanks dude!

@wayanjimmy

This comment has been minimized.

Copy link

wayanjimmy commented Jan 10, 2016

Thank you 👍

@hallfox

This comment has been minimized.

Copy link

hallfox commented Jan 12, 2016

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
@akshithg

This comment has been minimized.

Copy link

akshithg commented Mar 6, 2016

👍

@jsmestad

This comment has been minimized.

Copy link

jsmestad commented Mar 23, 2016

@hallfox thanks for the fish config 👯

@nicksarafa

This comment has been minimized.

Copy link

nicksarafa commented Mar 28, 2016

👍 thanks for the help.

For those like myself unfamiliar with go, you might have to run your go get commands with -d tag if you're running into the issue can't load package: package .: no buildable Go source files in while trying to install your package (Stack Overflow Reference).

@channprj

This comment has been minimized.

Copy link

channprj commented Mar 29, 2016

Thanks for your help.

@gukj

This comment has been minimized.

Copy link

gukj commented Apr 6, 2016

I get the following error after inserting path and GitHub username.

mkdir: /Users/username/GitHub/Go: File exists /Users/username/Downloads/77e6[...]b8b9c/install.sh: line 26: syntax error: unexpected end of file

Any idea what this means?

@mjreed09

This comment has been minimized.

Copy link

mjreed09 commented May 6, 2016

I'm getting that error as well.
line 26: syntax error: unexpected end of file

Any ideas?

@xavipedrals

This comment has been minimized.

Copy link

xavipedrals commented May 8, 2016

package golang.org/x/tools/cmd/vet: cannot find package "golang.org/x/tools/cmd/vet" in any of:
/usr/local/opt/go/libexec/src/golang.org/x/tools/cmd/vet (from $GOROOT)
/Users/xavi/golang/src/golang.org/x/tools/cmd/vet (from $GOPATH)

Anyone else with this error?

@nonotest

This comment has been minimized.

Copy link

nonotest commented May 13, 2016

@mjreed09 and @xavipedrals
try with

command -v brew >/dev/null 2>&1 || { ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"; };

its missing semicolons

@rhamedy

This comment has been minimized.

Copy link

rhamedy commented May 19, 2016

Thank you for the help and instructions @vsouza, very useful.

A couple of points based on my experiencing of carrying out these instructions on OSX 10.11.3

  1. I could not find .bashrc so I placed the exports inside .bash_profile
  2. I already had homebrew and git and when I run the ./install.sh it upgraded homebrew and give error while trying to install git because it was already installed.
  3. At the end I got the following

package golang.org/x/tools/cmd/vet: cannot find package "golang.org/x/tools/cmd/vet" in any of: /usr/local/opt/go/libexec/src/golang.org/x/tools/cmd/vet (from $GOROOT) /Users/Raf/workspace/golang/src/golang.org/x/tools/cmd/vet (from $GOPATH)

I checked /users/Raf/workspace/golang/src/golang.org/x/tools/cmd/vet and it exist, so not sure why the error if it already exist.

I tried the go run helloworld.go and I got the result.

@rmldsky

This comment has been minimized.

Copy link

rmldsky commented Aug 3, 2016

@hacfi

This comment has been minimized.

Copy link

hacfi commented Aug 15, 2016

I didn’t want to manually edit the paths in the .*rc so I added the following to the install.sh:

grep GOPATH ~/.zshrc || sed "s|^export GOPATH=.*|export GOPATH=${gopath}|g" >> ~/.zshrc <<EOF
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/golang
export PATH=\$PATH:\$GOPATH/bin
EOF

It’s not perfect but works for me. I only use zsh..maybe someone has time to do like if $SHELL = 'zsh' .. else ..

@lojunren

This comment has been minimized.

Copy link

lojunren commented Aug 28, 2016

Great Job! Thank U!

@a0s

This comment has been minimized.

Copy link

a0s commented Sep 12, 2016

@rmldsky
According to https://golang.org/doc/go1.2#go_tools_godoc :

$ go get code.google.com/p/go.tools/cmd/godoc
package code.google.com/p/go.tools/cmd/godoc: unrecognized import path "code.google.com/p/go.tools/cmd/godoc" (parse https://code.google.com/p/go.tools/cmd/godoc?go-get=1: no go-import meta tags)
$ go get code.google.com/p/go.tools/cmd/vet
package code.google.com/p/go.tools/cmd/vet: unrecognized import path "code.google.com/p/go.tools/cmd/vet" (parse https://code.google.com/p/go.tools/cmd/vet?go-get=1: no go-import meta tags)
@a0s

This comment has been minimized.

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

This comment has been minimized.

Copy link

kathgironpe commented Jan 7, 2018

Found that you don't need to install godoc

@Gerst20051

This comment has been minimized.

Copy link

Gerst20051 commented Apr 7, 2019

Why is brew install mercurial installed?

@vsouza

This comment has been minimized.

Copy link
Owner Author

vsouza commented May 28, 2019

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

@Shosta

This comment has been minimized.

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

This comment has been minimized.

Copy link

syafilm commented Oct 20, 2019

thanks a lot this gist save my life lol

@love2d-lua

This comment has been minimized.

Copy link

love2d-lua commented Dec 12, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.