Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set $GOPATH on Mac OSX

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?

like image 814
sergserg Avatar asked Feb 01 '14 14:02

sergserg


People also ask

How do I change my Gopath on Mac?

The GOPATH and PATH environment variables The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory ( $HOME/go ). If you really want to change your GOPATH to something else add GOPATH to your shell/bash/zsh initialization file .

How do I set Gopath and Gobin on Mac?

Go is installed in '/usr/local/go/bin/go'. And I set the GOPATH as my project src directory. I am able to run go code inside my directory.

Where can I find Goroot on Mac?

However, GOROOT is the place where the Go binary distributions assume they will be installed (so in Linux distributions, it's normally in /usr/local/go , but for MacOS, the Go and the tools are installed in /usr/local/Cellar/go/1.13/x.


1 Answers

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.

like image 99
elithrar Avatar answered Sep 27 '22 16:09

elithrar