Set GOPATH Variable in Linux & Windows PROPERLY

Understanding GO Workspace

In our previous article we explored the go workspace in detail, in this article I will just cover the steps to set GOPATH variable.

GOPATH is an environmental variable that specifies the location of your workspace. Typically Go programmers keep all their code in a single workspace. This workspace contain directory hierarchy with three main directories at its root, namely pkg, src and bin. The GOPATH environment variable is an important part for writing Go code without Go modules.

  • pkg: The pkg directory will contain the packages and shared object files if there are any. This objects are compiled from the src directory Go source code packages.
  • src: The src directory contains your Go source files. A source file is a file that you write your  Go code into. The source file will be used by the Go compiler to create an executable binary file. The src directory typically contains multiple version control repositories such as Git. This allows for a canonical import of code in your projects. This canonical import references a fully qualified package.
  • bin: The bin directory contains executable commands. The executables are binary files that run in your system and execute tasks.


How to organize codes in GO

When you choose not to use Go modules, you will need to configure your GOPATH. As mentioned earlier, the workspace contains the three directories namely pkg, src and bin. To add the code to your workspace, create another directory inside the src directory with the name of your version control system e.g Move inside your version control folder and add another directory containing the repository username .Th repository username will host all your Go repositories in that respective version control. Below is an example of the workspace structure.

Set GOPATH Variable in Linux & Windows PROPERLY

To get access to your GOPATH, ensure you have Go runtime installed in your machine. You can view your current GOPATH by typing go env GOPATH in your terminal for both Linux systems and Windows. You can also get access to all Go variables that are set by Go after a successful installation. You can see all these other Go related variables by entering the go env command in your terminal.


$ go env

When Go is installed in your machine , the GOPATH is set by default as of Go 1.8 . The GOPATH will be set to $HOME/go on Unix systems and %USERPROFILE%\go (Windows). It is worth mentioning that since Go version 1.11, you do not have to use GOPATH anymore.


Set GOPATH variable in Linux

Go path can be any directory in your system . In Unix for example, we set the GOPATH to $HOME/go which is the default path. Setting the GOPATH is done in a bashrc file. There are different bash files that can be present on one’s system , on Ubuntu or the linux based operating systems. It is recommended to use bashrc which can be opened by issuing the below command in your terminal. In this article we will set our GOPATH to /home/<username>/GoProjects. This workspace will host pkg, src and bin directory. The src directory will contain the sub-directory .

The workspace structure will look like below.


Set GOPATH Variable in Linux & Windows PROPERLY

With our workspace created enter the below commands to set the GOPATH.

$ nano  ~/.bashrc

Add the below commands at the bottom of the .bashrc file and save the file.

export GOPATH=$HOME/GoProjects
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin


Set GOPATH Variable in Linux & Windows PROPERLY

To save these changes press ctrl + O then press ENTER then ctrl + X. These changes will take effect after restarting your Linux machine. Just to confirm that the go path has been set, issue the below command after restarting you machine

$ go env GOPATH




Set GOPATH variable in Windows (Using GUI)

After successfully installing Go in your windows system, create a folder anywhere in your system like the Projects folder for example. This new folder will be your workspace. Add bin, src and pkg directories in your workspace. Please follow the below steps.

  1. Go to Control Panel → All Control Panel Items → System → Advanced System Settings → Advanced → Environmental Variables.
  2. Add new system variable by clicking New on System variables → Variable name = GOPATH , Variable value = C:\Projects\Go
  3. RESTART your cmd . Your GOPATH in Windows is now set.

To check if the path has been set correctly, enter echo %GOPATH% in the command line.


Set GOPATH variable in Windows (Using cmd)

  1. Open a command prompt by pressing win + r the enter cmd or use powershell window by pressing win + i
  2. Type go env -w GOPATH=c:\Projects


Setting GOPATH in Windows 10

  1. Open a command prompt by pressing win + r the enter cmd or use powershell window by pressing win + i
  2. Enter setx GOPATH %USERPROFILE%\go. (This will set the GOPATH to your <home folder>\go, such as C:\Users\yourusername\go.)
  3. Close the command  line . Please note that this changes will take effect on a new command line or Powershell.



GOPATH is used when a Go developer does not want to use the Go Modules. When setting a GOPATH, it should lead to a workspace with three directories namely pkg, bin, src. The source directory hosts several version control repositories that can be managed by  Git. The path to the package's directory determines its import path.




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