GO is a general-purpose system programming language which means that you can build a wide variety of application using it. It is purely an open-source language developed by Google. It is cross-platform, which supports all major operating systems.
Go source code is compared to a binary executable or library, this results in very high performance when running Go application. Compilation speed for the Go applications are really fast. In a nutshell, Go is an elegant language with a clean and concise specifications that are readable and comprehensive. One of the major strengths of Golang is its concurrency, which means multiple processes of the Go applications can run at same time.
In this article, I'll explain how to install Go language on our latest Linux distributions of CentOS 7 and Ubuntu.
Install Go on CentOS 7
It is quite easy to install Go Language in Redhat Based distributions too. It is available in their base repository packages. We can install it by just running this command below:
$ sudo yum install golang
it will install all required packages for this language.
You can confirm with the Go version installed.
$ sudo go version go version go1.4.2 linux/amd64
We can manage the Go source codes using the "Go" tool. There are many commands which can be used with Go tool. Here are the list of them.
We can get more information regarding each command usage by executing "go command help" like go build help or go install help.
You can create a work folder in this installation too, which will help you to build and install its binaries. Furthermore, create the environment variables server-wide..
$ sudo mkdir ~go $ sudo source /etc/profile.d/goenv.sh $ sudo cat /etc/profile.d/goenv.sh export GOROOT=/usr/lib/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Install Go language on Ubuntu 16.04
Go language and its tool kits are available in our base repositories in all the major operating systems. We can install Go language in Ubuntu by just running this command.
$ sudo apt-get install golang $ sudo go version go version go1.6.1 linux/amd64
Now, we need to place the Go codes inside a work directory where, we can build the Go tools and install its binaries. I created a directory for Go codes in /home folder.
$ sudo mkdir /home/go
Create a file "/etc/profile.d/goenv.sh" for setting up Go environment variable server-wide as below:
$ sudo cat /etc/profile.d/goenv.sh export GOROOT=/usr/lib/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin $ sudo source /etc/profile.d/goenv.sh
Install Go from Source
If you notice, in the above installations, you can see that the versions of Go language packages installed are different in two distributions. This means that it's not mandatory to have the latest versions available on our base repository packages. So whenever we need to install the latest package, we can download it direct from the source.
Depending on our server architecture, we can download the required package and extract to install.
$ sudo wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz 2016-07-01 07:50:26 (93.6 MB/s) - ‘go1.6.2.linux-amd64.tar.gz’ saved [84840658/84840658] $ sudo tar -xzvf go1.6.2.linux-amd64.tar.gz -C /usr/local/
I've downloaded the package for a 64 bit architecture. You can create a work folder set environment variables server-wide as before.
$ sudo mkdir ~go $ sudo cat /etc/profile.d/goenv.sh export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin $ sudo source /etc/profile.d/goenv.sh $ sudo go version go version go1.6.2 linux/amd64
The only difference in creating environment variable is that, here the Go libraries reside inside our /usr/local folder comparing the above cases.
A Simple program in Go
Now we can test our installation by creating a test program. Our first sample program will print the “hello world” message. Create a file to print "helloworld.go".
Now we need to run this program using go command.
$ sudo go run helloworld.go hello world
At times, we’ll need to build our programs into binaries. We can use build command for that.
$ sudo go build helloworld.go $ ls helloworld helloworld.go
We can then execute the built binary directly like this.
$ sudo ./helloworld hello world
If this works, means you've built your Go successfully :)
You can get more examples of this program to start learning. I hope you enjoyed reading this article. I would recommend your valuable suggestions and comments on this. Have a Good day!