In previous article, we have introduced golang
environment.
In today’s post, we will discuss about GOPATH, GOROOT and the
differences between two definitions.It is important to understand how
the Go compiler looks for the locations of the packages that are used in
our applications.
The Go compiler needs a way to know how to find our source files
(packages) so that the compiler can build and install them. The compiler
utilizes two environmental variables for this
job.$GOROOTand$GOPATHtell the Go compiler where to search for
the locations of the Go packages listed by theimportstatement.
GOPATH in Golang
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.
GOPATHmust be set to get, build and install packages outside the
standard Go tree.
$GOPATHis the location for packages we create and third-party packages that we may have imported.
If the environment variable is unset, GOPATH defaults to a
subdirectory named “go” in the user’s home directory ($HOME/go on
Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go
distribution. Run “go env GOPATH” to see the current GOPATH.

Setting GOPATH
You can modify the GOPATH environment variable to utilize a specific
location as your workspace. How to set this variable on various
platforms is described on this page
In Unix systems
Any directory on your system may be used as GOPATH. In instances using
Unix, we’ll set it to $HOME/go (the default since Go 1.8). Keep in
mind that your Go installation’s path and GOPATH cannot both be the
same. Another typical configuration is GOPATH=$HOME.
Go 1.13+
go env -w GOPATH=$HOME/go
Bash
Edit ~/.bash_profile of your user to add the following line in the
last:
export GOPATH=$HOME/go
After saving, close your editor. Source ~/.bash_profile after that:
source ~/.bash_profile
In Windows
You can choose where your workspace is, but for the purposes of this example, let’s choose C:\go-work.
GOPATH cannot be the same.
- Create
C:\go-workfolder - Control Panel can be accessed by doing a right-click on “
Start.” Click “System” then “System and Security” - Go to “
Advanced system options” in the menu on the left. - At the bottom, select “
Environment Variables” - In the “
User variables” section, click “New” - In the “
Variable name” field, enter GOPATH. - In the “
Variable value” field, enterC:\go-work. - Select
OK.
Go 1.13+ (command line)
- Access command prompt (
Win+rthen typecmd) or a powershell window (Win+i). - Run
go env -w GOPATH=c:\go-work.
Directory structure in GOPATH
Each directory listed in GOPATH must have a prescribed structure:
- The src directory holds source code. The path below
srcdetermines the import path or executable name. - The pkg directory holds installed package objects. As in the Go
tree, each target operating system and architecture pair has its own
subdirectory of
pkg(pkg/GOOS_GOARCH). If DIR is a directory listed in the GOPATH, a package with source inDIR/src/foo/barcan be imported as “foo/bar” and has its compiled form installed to “DIR/pkg/GOOS_GOARCH/foo/bar.a”. - The directory holds compilbined commands. Each command is named
for its source directory, but only the final element, not the entire
path. That is, the command with source in
DIR/src/foo/quuxis installed intoDIR/bin/quux, notDIR/bin/foo/quux. The “foo/” prefix is stripped so that you can addDIR/binto your PATH to get at the installed commands. If the GOBIN environment variable is set, commands are installed to the directory it names instead ofDIR/bin. GOBIN must be an absolute path.
For example, if we have a package located
at$GOPATH/src/person/address/and we want to use the address
packages, we would need the followingimportstatement:
import "person/address"
Another example would be if we have a package
at$GOPATH/src/company/employee. If we were interested in using
theemployeepackage, theimportstatement would be as follows:
import "company/employee"
Here’s an example directory layout:
GOPATH=/home/goLinuxCloud/go
/home/goLinuxCloud/go/
+---bin
| test_bin.exe (installed command)
|
+---pkg
| \---github.com
| \---spf13 (installed packages)
\---src
\---greeter
+---helper (go code in package helper)
| helper.go
|
\---main (go code in package main)
main.go
GOROOT in Golang
A second environment variable,GOROOT, specifies the root directory of
the Go distribution, which provides all the packages of the standard
library. The directory structure beneathGOROOTresembles that
ofGOPATH, so, for example, the source files of thefmtpackage reside
in the$GOROOT/src/fmtdirectory. Users never need to
setGOROOTsince, by default, thegotool will use the location where it
was installed.
In order to check the current GOROOT enter the following command:
go env GOROOT
Output:

The Go binary distributions presume that the Go tools will be installed
at /usr/local/go (or C:\Go under Windows), but you can choose to
install them somewhere else. The GOROOT environment variable must be
set in this situation to point to the location where it was installed.
For Linux environment, you should add the following commands to
$HOME/.profile, for instance, if you installed Go in your home
directory:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
For windows, you can follow the steps as modify the GOPATH
environment.
GOROOTmust be set only when installing to a custom location.
Summary
In this article, I have shown you 2 important environment variables when
writing Golang code: GOPATH and GOROOT. So, in short:
GOROOTis for compiler/tools that comes from go installation such as fmt. sync, io, strings and so onGOPATHis for your own go projects / 3rd party libraries (downloaded with “go get”).
What’s Next
Set GOPATH Variable in Linux & Windows PROPERLY
References
https://pkg.go.dev/cmd/go#hdr-GOPATH_environment_variable
https://go.dev/doc/install#tarball_non_standard


