Golang GOPATH vs GOROOT Comparison

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. $GOROOT and $GOPATH tell the Go compiler where to search for the locations of the Go packages listed by the import statement.



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. 

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 GOPATH.

Golang GOPATH vs GOROOT Comparison


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


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.


The directory where you install Go and GOPATH cannot be the same.
  • Create C:\go-work folder
  • 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, enter C:\go-work.
  • Select OK.

Go 1.13+ (command line)

  • Access command prompt (Win + r then type cmd) 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 src determines 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 in DIR/src/foo/bar can 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/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The "foo/" prefix is stripped so that you can add DIR/bin to 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:

import "person/address"

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:

import "company/employee"

Here's an example directory layout:

|       test_bin.exe                      (installed command)
|   \---github.com
|       \---spf13                         (installed packages)
        +---helper                        (go code in package helper)
        |       helper.go
        \---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


Golang GOPATH vs GOROOT Comparison

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.


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: GOPATH and GOROOT. So, in short:

  • GOROOT is for compiler/tools that comes from go installation such as fmt. sync, io, strings and so on
  • GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").


What’s Next

Set GOPATH Variable in Linux & Windows PROPERLY





Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment