Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the GOPATH environment variable on Ubuntu?
Before setting up GOPATH or GOROOT on your local environment, we must verify that Go is correctly installed on your system.
Type the following command in your terminal to check the Go installation −
go version
If it outputs nothing or displays "go: command not found", then you need to first download the Go binary and install it before setting up the GOPATH.
When Go is properly installed, the output will look something like this −
immukul@192 linux-questions-code % go version go version go1.16.3 darwin/amd64
Understanding GOPATH
GOPATH is an environment variable that tells the Go tools where to find your workspace. It should point to a directory containing three subdirectories −
src − Contains your Go source code files
pkg − Contains compiled package objects
bin − Contains executable binaries
Setting GOPATH on Ubuntu
To set the GOPATH environment variable, you need to edit your shell configuration file. On Ubuntu, this is typically ~/.bashrc.
Open the bashrc file using a text editor −
vi ~/.bashrc
Add the following lines at the end of the file −
export GOPATH=/home/$USER/go_projects export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save the file and reload the configuration −
source ~/.bashrc
Creating GOPATH Directory Structure
Create the workspace directory and its subdirectories −
mkdir -p $GOPATH/src $GOPATH/pkg $GOPATH/bin
Environment Variables Explanation
| Variable | Purpose | Example Value |
|---|---|---|
| GOPATH | Your workspace directory | /home/user/go_projects |
| GOROOT | Go installation directory | /usr/local/go |
| PATH | Executable search paths | Includes $GOROOT/bin and $GOPATH/bin |
Verification
Verify that your environment variables are set correctly −
echo $GOPATH echo $GOROOT go env GOPATH
Expected output −
/home/user/go_projects /usr/local/go /home/user/go_projects
Conclusion
Setting up GOPATH correctly is essential for Go development on Ubuntu. The GOPATH environment variable defines your workspace structure with src, pkg, and bin directories. Along with GOROOT and PATH updates, this provides a complete Go development environment ready for building applications.
