Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

Tags:

linux

go

ubuntu

I am trying to do a go get:

go get github.com/go-sql-driver/mysql 

and it fails with the following error:

package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath 

when I do a go env , a list of Go values is shown as below:

ubuntu@ip-xxx-x-xx-x:~$ go env GOARCH="amd64" GOBIN="" GOCHAR="6" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="" GORACE="" GOROOT="/usr/lib/go" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" CC="gcc" GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread" CGO_ENABLED="1" 

clearly the GOPATH is not set, how and where do I set it?

I see many threads that mention this error but none that provide an answer to my question, which file needs to be edited to provide a value for this path?

like image 917
David Saintloth Avatar asked Jan 08 '14 16:01

David Saintloth


People also ask

What should Gopath be set to?

The GOPATH environment variable It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go ) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory.

What's the Gopath environment variable?

GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows). GOPATH stores your code base and all the files that are necessary for your development.


2 Answers

New Way:

Check out this answer.

Old Way:

Just add the following lines to ~/.bashrc and this will persist. However, you can use other paths you like as GOPATH instead of $HOME/go in my sample.

export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin 
like image 120
Daniel Lin Avatar answered Oct 05 '22 07:10

Daniel Lin


New Way: Go Modules

Since Go 1.11, you don't have to use GOPATH anymore. Simply go to your project directory and do this once:

go mod init github.com/youruser/yourrepo 
  • With this, Go creates a module root at that directory.
  • You can create as many modules as you want.
  • For step by step instructions, also see this answer.

Old Way: GOPATH

If you insist on working with GOPATH then heed this:

  • Since Go 1.8, you don't need to set your GOPATH or GOROOT.
  • GOPATH by default is under your user/home directory.

From the documentation:

If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.

like image 35
Inanc Gumus Avatar answered Oct 05 '22 06:10

Inanc Gumus