In previous article, we have introduced golang environment. In today's post, we will discuss about
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.
$GOPATH tell the Go compiler where to search for the locations of the Go packages listed by the import statement.
GOPATH in Golang
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.
GOPATH must be set to get, build and install packages outside the standard Go tree.
$GOPATH is 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
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
go env -w GOPATH=$HOME/go
~/.bash_profile of your user to add the following line in the last:
After saving, close your editor. Source
~/.bash_profile after that:
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.
- 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 "
- In the "
User variables" section, click "
- In the "
Variable name" field, enter GOPATH.
- In the "
Variable value" field, enter
Go 1.13+ (command line)
- Access command prompt (
cmd) or a powershell window (
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/GOOS_GOARCH). If DIR is a directory listed in the GOPATH, a package with source in
DIR/src/foo/barcan be imported as "
foo/bar" and has its compiled form installed to "
- 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 into
DIR/bin/foo/quux. The "
foo/" prefix is stripped so that you can add
DIR/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 of
DIR/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 following import statement:
Another example would be if we have a package at
$GOPATH/src/company/employee. If we were interested in using the employee package, the import statement would be as follows:
Here's an example directory layout:
| test_bin.exe (installed command)
| \---spf13 (installed packages)
+---helper (go code in package helper)
\---main (go code in package main)
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 beneath GOROOT resembles that of GOPATH, so, for example, the source files of the
fmt package reside in the
$GOROOT/src/fmt directory. Users never need to set GOROOT since, by default, the go tool will use the location where it was installed.
In order to check the current GOROOT enter the following command:
go env GOROOT
The Go binary distributions presume that the Go tools will be installed at
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:
For windows, you can follow the steps as modify the
GOROOT must be set only when installing to a custom location.
In this article, I have shown you 2 important environment variables when writing Golang code:
GOROOT. So, in short:
GOROOTis for compiler/tools that comes from go installation such as fmt. sync, io, strings and so on
GOPATHis for your own go projects / 3rd party libraries (downloaded with "