- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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
Setting the GOPATH
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.
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.
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.
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.
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.
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.
Now just source the bashrc file with the help of the command shown below −
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 −
The output must be the path of the directory that you set as an environment variable.
- How to set python environment variable PYTHONPATH on Linux?
- How to set python environment variable PYTHONPATH on Mac?
- How to set python environment variable PYTHONPATH on Windows?
- How do we set the Java environment variable in cmd.exe?
- How to substitute an environment variable using sed on Linux?
- How to Set-Up Shiny Server on Ubuntu
- How to set up openvpn on ubuntu 16 04
- How to set up your python development environment on AWS?
- How To Set Up and Configure NFS on Ubuntu 16.04
- How to get the value of environment variable in Postman?
- How to set JAVA_HOME environment variables on Windows OS in Java?
- How to get environment variable value using PowerShell?
- How To Configure and Set Up a Firewall on Ubuntu 16.04
- How To Set-Up Apache to use Password Authentication on Ubuntu 16.04
- How to Set environment variables using PowerShell?