Go, also known as GoLang, is an open source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007. The Go language became a public open source project on November 10, 2009. Go language is being actively used in some Google’s production servers, as well as by other companies such as Dropbox, CoreOS, CloudFlare, Docker, Soundcloud, Uber and many. In this tutorial, we will see how to install and configure Go programming language in Linux.
You can install Golang either using package manager or compile and install from source. We will see both.
Go language is available in the default repositories of most modern Linux distributions.
Go is available in the default repositories of some Linux distributions. However, the Go language version in the default repositories might be bit outdated. So, it is always recommended to use the most recent version by downloading the tarball from the official website and manually install it as described below.
The following were tested in Ubuntu 16.04 LTS server edition. However, these steps are same for other Linux distributions.
Download the latest tarball from here.
Next, check the integrity of the downloaded file as shown below.
Please note that the SHA256 Checksum value displayed above should match with the one provided with the download link. If it doesn’t match, download new tarball and proceed.
This command extracts the tarball to /usr/local directory. Here, -c flag indicates the target directory.
Next, setup workspace for Go language where we are going to save the Go language builds.
Here, $HOME/go_projects is the directory which is where Go will build its files.
Next, we need to point Go to the new workspace.
Append the following lines to point Go to the new workspace directory.
If Go language is installed on another location other than the default location (/usr/local/), you need to specify the installation path(GOROOT) in the ~/.profile file. Say for example if you have installed go lang in your HOME directory, then you will have to add the following lines in user profile file.
Please note that if you have installed Golang using package managers, the installation path will be either /usr/lib/go or /usr/lib/golang. You need to update the correct path value in GOROOT.
Once you have specified the appropriate values, run the following command to update Go lang environment values.
Now, run the following commands to verify if you have properly installed and configured Go language.
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build286572886=/tmp/go-build -gno-record-gcc-switches"
If you see output something like above, congratulations! Go language is ready to use. You can start coding your go programs.
We know now how to install and configure Go language. Let us go ahead and create a simple ‘hello world’ program.
Create a separate directory to store the source code of your program. The following command creates a directory to store the ‘hello world’ program source code.
Also, check the Go language documentation link provided at the end of this guide for more details. In case you don’t want Go language anymore, you can uninstall by simply removing the go directory i.e /usr/local/go. Also, delete the workspace directories as well.
That’s all for now. In this guide, you have learned how to install Go language in Linux and how to configure and code a simple program. If you find this guide useful, please share it on your social, professional networks and support OSTechNix. More good stuffs to come. Stay tuned!