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

go, linux, ubuntu

Solution

New Way:

Check out this answer.

Note: Not for trying out a go application / binaries on your host machine using `go install [repo url]`, in such cases you still have to use the old way.

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

Problem

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?

Original source

Related problems