GO is a general purpose system programming language which means that you can build wide variety of application using it. It is purely an open source language developed by Google. It has cross platform, which supports all major operating systems.
Go source code is compared to a binary executable or library, this results in a 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 process 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 Ubuntu and CentOS.
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.
root@ubuntu:~# apt-get install golang root@ubuntu:~# 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.
root@ubuntu:~# mkdir /home/go
Create a file "/etc/profile.d/goenv.sh" for setting up Go environment variable server-wide as below:
root@ubuntu:~# cat /etc/profile.d/goenv.sh export GOROOT=/usr/lib/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin root@ubuntu:~# source /etc/profile.d/goenv.sh
Install Go language on CentOS 7
As I explained before, 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:
[root@localhost ~]# yum install golang
it will install all required packages for this language.
You can confirm with the Go version installed.
[root@localhost ~]# 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..
[root@Centos7 ~]# mkdir ~go [root@Centos7 ~]# source /etc/profile.d/goenv.sh [root@Centos7 ~]# cat /etc/profile.d/goenv.sh export GOROOT=/usr/lib/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Install latest version 1.6 from Source
If you notice, in the above installations, you can see that the versions of Go language packages installed is 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 and install. Let's see how to do that.
Depending on our server architecture, we can download the required package and extract to install.
[root@server1 src]# 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] [root@server1src]# 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.
root@server1~]# mkdir ~go [root@Centos7 ~]# cat /etc/profile.d/goenv.sh export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin [root@s ~]# source /etc/profile.d/goenv.sh [root@server1 bin]# 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.
[root@Centos7 ~]# go run helloworld.go hello world
At times, we’ll need to build our programs into binaries. We can use build command for that.
[root@Centos7 ~]# go build helloworld.go [root@Centos7 ~]# ls helloworld helloworld.go
We can then execute the built binary directly like this.
[root@Centos7 ~]# ./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!