86

I'm trying to set my $GOPATH variable to run some example code on my machine:

$ smitego-example go run main.go 
main.go:5:2: cannot find package "github.com/#GITHUB_USERNAME#/smitego" in any of:
    /usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego (from $GOROOT)
    ($GOPATH not set)

$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': not a valid identifier

enter image description here

Contents of github.com/#GITHUB_USERNAME#/smitego/smitego.go:

package smitego

How can I set my GOPATH so it works always and forever?

Joshua Cook
  • 12,495
  • 2
  • 35
  • 31
sergserg
  • 21,716
  • 41
  • 129
  • 182

9 Answers9

147

Update, as of Go 1.8: If you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you.

It defaults to $HOME/go on macOS (nee OS X) - e.g. /Users/matt/go/. This makes getting started with Go even easier, and you can go get <package> right after installing Go.


For the shell: (the manual method)

~/.bash_profile should contain export GOPATH=$HOME/go and also export PATH=$GOPATH/bin:$PATH. The use of the $ is important: make sure to note where I've used it (and where I have not).

For Sublime Text:

Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User

{
        "shell": ["/bin/bash"],
        "env": {"GOPATH": "/Users/#USERNAME#/go/"},
}

Make sure your GOPATH is not set to the full path of the package; just the root of your go folder where src, pkg, and bin reside. If you're not using GoSublime, I'd suggest installing that first.

elithrar
  • 23,364
  • 10
  • 85
  • 104
  • 4
    `/usr/bin/bash` is usually `/bin/bash` on Mac or Linux. – tidwall Jan 08 '15 at 21:22
  • in case this helps anyone if you install go via homebrew then the path will be like this: `export PATH=/usr/local/opt/go@1.12/bin:$PATH` (obviously update the version based on whatever you install) – Michael Sep 10 '20 at 06:06
55

The accepted answer didn't work for me. I investigated and found the cause: I am using zsh, not bash.

I need to add the following two lines to ~/.zshrc:

export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
18
  1. Download and install Go tools https://golang.org/doc/install

  2. Setup Go workspace

    mkdir $HOME/go && cd $HOME/go
    mkdir bin pkg src

  3. Setup Go environment

    sudo vi ~/.bash_profile
    export GOPATH=$HOME/go
    PATH=$PATH:$GOPATH/bin

Test by creating, building and running a Go project

mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello
Rimian
  • 36,864
  • 16
  • 117
  • 117
Hom Bahrani
  • 3,022
  • 27
  • 25
  • This is the best answer IMHO. I followed it verbatim and I was immediately writing go code. – pim Oct 02 '18 at 12:52
  • Is "sudo vi ~/.bash_profile" creating a new .bash_profile file? If yes then where this file should be created exactly? – Hank Moody Apr 24 '19 at 06:33
  • 1
    it will create it in the root directory. Note if you are using Oh My Zsh then you will have to add the go paths (step 3) to your `.zshrc` instead and then run `source .zshrc` if you want the changes to take effect without closing your terminal. Also you dont have to use vi ~/.bash_profile, you can open the file in atom or nano text editors as well – Hom Bahrani Sep 06 '19 at 08:53
  • 1
    this answer got me going! i'm getting back into mac development from doing windows 24/7 - so haven't used vim before - this helped, http://commandlinemac.blogspot.com/2008/12/vim.html – Poat Apr 10 '20 at 17:37
17

You don't put the $ prefix on a variable when you're assigning it, only when you're reading it.

export GOPATH=$HOME

To make this permanent, put the command in your .bash_profile.

That will work for Terminal shells. If you need to set environment variables that will affect GUI applications, see Environment variables in Mac OS X

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    On OS X and using Sublime might the user have to use launchctl? I'm not sure .bash_profile would matter for GUI apps. So might need to do: launchctl setenv GOPATH=/path/to/home, and to make it last between boots add that to /etc/launchd.conf – powerj1984 Feb 01 '14 at 14:49
  • Thanks! I learned something new today. However, now I'm getting: `cannot find package "github.com/sergiotapia/smitego" in any of: / /usr/local/go/src/pkg/github.com/sergiotapia/smitego /Users/sergiotapia/src/github.com/sergiotapia/smitego`. Any ideas? – sergserg Feb 01 '14 at 14:51
  • I don't know anything about `go`, so I can't help you with that part of it. My answer just shows the proper way to set environment variables; figuring out what you're supposed to set it to is a separate problem. – Barmar Feb 01 '14 at 14:55
  • 1
    @Serg does 'go build' work from the command line after modifying your .bash_profile and sourcing it/reloading the shell? – powerj1984 Feb 01 '14 at 15:00
  • what is the output of 'echo $GOPATH' ? – powerj1984 Feb 01 '14 at 15:10
  • 1
    can you take this discussion out of the comments on my answer? I don't wish to be notified of all these questions. – Barmar Feb 01 '14 at 15:14
  • @powerj1984: Output is: /Users/sergiotapia – sergserg Feb 01 '14 at 15:40
  • Ha ha :) this should have been the correct answer though. – Maniankara Apr 08 '18 at 05:25
5

The http://www.golang-book.com/guides/machine_setup#osx

only has instructions for setting the path on ~/.bashrc, not ~/.bash_profile which thanks to this thread was able to get my example file to build.

export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin

Other Mac users need to add the above to their ~/.bash_profile.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
uroborus
  • 51
  • 1
  • 3
  • This guide now uses a program I wrote (github.com/badgerodon/penv) which saves environment variables for OSX using launchctl (https://github.com/badgerodon/penv/blob/master/darwin_dao.go). AFAIK this is the only way to set environment variables reliably in OSX. – Caleb Jul 26 '15 at 21:27
  • Heavily advice against this, your `GOPATH` **IS** your home directory. I (and many other comments) recommend setting `GOPATH` to `$HOME/go` – jnovack Sep 24 '18 at 18:58
5

After installing go with brew or with package this solved my problem:

export GOROOT="/usr/local/go"
export GOPATH="$HOME/Documents/goWorkSpace"
export PATH="$HOME/Documents/goWorkSpace/bin:$PATH"
arctic_Oak
  • 974
  • 2
  • 13
  • 29
1

on macOS High Sierra Version 10.3.3, Go[go version go1.10.1 darwin/amd64] Installed here :

Go Install Location

Added following on :~/.bashrc

export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin

and then Go Works

Go Works

Sumit Arora
  • 5,051
  • 7
  • 37
  • 57
0

For my m2 macOs Ventura I updated ~/.zprofile

adding the lines

export GOPATH=/usr/local/go
export PATH=$GOPATH/bin:$PATH
micah
  • 838
  • 7
  • 21
-1

People working with the latest macs and above Catalina version, you guys need to update the .zshrc file instead of .bash.

Add the following two lines to ~/.zshrc:

export GOPATH=/Users/username/go

export PATH=$GOPATH/bin:$PATH

it should work.!!

This got change a while back, please refer to the link below to understand why .zshrc and not .bash_profile https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/

Ishan Kumar
  • 21
  • 1
  • 9