How to set the GOPATH environment variable on Ubuntu?

Before setting up GOPATH or GOROOT on the local environment of yours, we must check whether you have correctly installed Go or not.
Just type the following command on any user of the machine, where you think you have installed Go −
go version
If it outputs nothing or something like go is not present, then instead of setting the GOPATH first I would recommend that you first download the go binary from this link and then install it on your local machine.
Normally the output for the case that you have Go installed will look something like this −
immukul@192 linux-questions-code % go version
go version go1.16.3 darwin/amd64
GOPATH is nothing but an environment variable path that needs to be set that tells the Go binary to where to look for the code that you will be writing. In order to set the GOPATH, one must know how to set an environment variable in Linux based operating systems.
While there are different bash files that can be present on one’s system, on Ubuntu or other Linux based operating systems, it is generally recommended to make use of the bash_rc which you can open with typing the following command in your terminal.
vi ~/.bashrc
And if by any chance you are using mac os like me, then the following command will open the zshrc file, which does the same work for you as bashrc in Ubuntu.
vi ~/.zshrc
It is a good practice to note that the GOPATH can be set to any directory in your system, just make sure that Go is installed on that user if you are using Linux or macOS. To set GOPATH, open the bashrc/ bash_profle/zshrc file and type the following commands in it and then save the file.
export GOPATH=/root/go_projects
As you can clearly see in the above command where I am exporting a directory, I choose to keep my Go code inside the /root/go_projects directory. While you are free to choose any directory you like, it is recommended that you create a separate new folder for GOPATH. Now inside the go_projects folder, we need to have the three subfolders, mainly −
pkg − the directory that will contain the packages and shared object files if any.
pkg − the directory that will contain the packages and shared object files if any.
src − the directory where all the code you will write will be stored.
src − the directory where all the code you will write will be stored.
bin − the directory which will contain all the binary executables that you will make.
bin − the directory which will contain all the binary executables that you will make.
While there are three directories that must be present inside the GOPATH folder, we usually end up spending most of our time inside the src directory where the code that you will write will go.
Setting the GOROOT is also important. To do that we need to open the bashrc file again and type in the below command and then source it.
export GOROOT=/usr/local/go
Now just source the bashrc file with the help of the command shown below −
source ~/.bashrc
It is always a good habit to check whether your environment variables are set properly or not and you can do that by just typing the following command to your terminal −
echo $GOPATH
The output must be the path of the directory that you set as an environment variable.
/root/go_projects