What should be the values of GOPATH and GOROOT?
GOPATH
is discussed in the cmd/go
documentation:
The
GOPATH
environment variable lists places to look for Go code. On
Unix, the value is a colon-separated string. On Windows, the value is
a semicolon-separated string. On Plan 9, the value is a list.
GOPATH
must be set to get, build and install packages outside the
standard Go tree.
GOROOT
is discussed in the installation instructions:
The Go binary distributions assume they will be installed in
/usr/local/go
(orc:\Go
under Windows), but it is possible to install
the Go tools to a different location. In this case you must set the
GOROOT
environment variable to point to the directory in which it was
installed.For example, if you installed Go to your home directory you should add
the following commands to$HOME/.profile
:export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Note:
GOROOT
must be set only when installing to a custom location.
(updated version of Chris Bunch's answer.)
When should you set the $GOPATH variable?
From the documentation:
The Go path is used to resolve import statements.
The GOPATH environment variable lists places to look for Go code.
When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).
Assuming you're using modules, which you probably should be, the setting of GOPATH will not impact your projects regardless of where you put them. It only needs to be set somewhere so the Go tools have a cache to store libraries and binaries.
Setting GOPATH on Ubuntu - still getting error
Okay guys, I figured out the problem.
https://github.com/joefitzgerald/go-plus/issues/386
here it has been discussed. I will just copy it from that
Linux folks, particularly those running Ubuntu. I believe I have reproduced your issues and I think they have to do with where you are setting your GOPATH
and PATH
.
~/.profile
: If you set them here,$GOPATH
will be set in Atom when you launch Atom for the first time from the launcher in the toolbar~/.bashrc
If you set them here,$GOPATH
will be set in Atom when you launch Atom for the first time from the terminal
Thus, to ensureGOPATH
andPATH
are set correctly regardless of how you launch Atom, you should put the following (or similar) in both~/.profile
and~/.bashrc
:
export GOPATH=$HOME/work
export PATH=$GOPATH/bin:/usr/local/go/bin:$PATH
Obviously the above doesn't apply exactly if bash isn't your default shell, but I trust that if you're a user of a different shell, you can grok what I am saying above and apply the same principles to your particular environment.
Can I set the GOPATH using the go command line tool?
Instead of trying to have a GOPATH per project to separate dependencies, work with Go modules.
A short intro on Go modules
: https://ncona.com/2020/10/introduction-to-golang-modules/
Indepth, official intro: https://blog.golang.org/using-go-modules
My intro:
In your repository call go init yourModuleName
to start working with go modules.
Once initialized, to me the most important command is go mod tidy
. You call that and it cleans up your go.mod/go.sum files, removing what is not needed and adding what is needed.
To add a new dependency call go get dependencyname
from within your project folder to add it to your go.mod
file and be able to use it in your code.
To update a dependency, just call go get dependencyname
again and it will update the version to the latest available in go.mod
file.
Related Topics
Performing Http Requests With Curl (Using Proxy)
Appending a Line to a File Only If It Does Not Already Exist
Sorting Multiple Keys With Unix Sort
Linux Default Behavior of Executable .Data Section Changed Between 5.4 and 5.9
How to Ensure Only One Instance of a Bash Script Is Running
How to Compile a 32-Bit Binary on a 64-Bit Linux Machine With Gcc/Cmake
How to Remove ^[, and All of the Escape Sequences in a File Using Linux Shell Scripting
Bash Script Process Substitution Syntax Error: "(" Unexpected
The Difference Between Fork(), Vfork(), Exec() and Clone()
Redirect Stderr/Stdout of a Process After It's Been Started, Using Command Line
How to Access Physical Addresses from User Space in Linux
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Error:13 - Permission Denied Android Studio