Reputation: 22224
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
Contents of github.com/#GITHUB_USERNAME#/smitego/smitego.go
:
package smitego
How can I set my GOPATH
so it works always and forever?
Upvotes: 89
Views: 177000
Reputation: 1
This worked for me. https://hostman.com/tutorials/how-to-install-go-on-macos/
echo "export GOROOT=/usr/local/go" >> .bash_profile
echo "export GOPATH=$HOME/Documents/go" >> .bash_profile
echo "export PATH=$GOPATH/bin:$GOROOT/bin:$PATH" >> .bash_profile
vi main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!") // CONCLUSION: Hello, World!
}
go run main.go
Upvotes: 0
Reputation: 14318
add:
export GOPATH=/Users/$USER/go
export PATH=$GOPATH/bin:$PATH
into your boot script (normally: ~/.bashrc
or ~/.zshrc
)
Upvotes: 1
Reputation: 3532
Download and install Go tools https://golang.org/doc/install
Setup Go workspace
mkdir $HOME/go && cd $HOME/go
mkdir bin pkg src
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
Upvotes: 19
Reputation: 21
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/
Upvotes: -1
Reputation: 1024
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"
Upvotes: 5
Reputation: 5241
on macOS High Sierra Version 10.3.3, Go[go version go1.10.1 darwin/amd64] Installed here :
Added following on :~/.bashrc
export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
and then Go Works
Upvotes: 2
Reputation: 24240
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.
Upvotes: 151
Reputation: 51
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
.
Upvotes: 5
Reputation: 20356
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
Upvotes: 64
Reputation: 780688
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
Upvotes: 18