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.
[email protected]:~# apt-get install golang
[email protected]:~# 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.
[email protected]:~# mkdir /home/go
Create a file "/etc/profile.d/goenv.sh" for setting up Go environment variable server-wide as below:
[email protected]:~# cat /etc/profile.d/goenv.sh
[email protected]:~# 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:
[[email protected] ~]# yum install golang
it will install all required packages for this language.
You can confirm with the Go version installed.
[[email protected] ~]# 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..
[[email protected] ~]# mkdir ~go [[email protected] ~]# source /etc/profile.d/goenv.sh [[email protected] ~]# cat /etc/profile.d/goenv.sh
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.
[[email protected] 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] [[email protected]]# 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.
[email protected]~]# mkdir ~go[[email protected] ~]# cat /etc/profile.d/goenv.sh
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin [[email protected] ~]# source /etc/profile.d/goenv.sh [[email protected] 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.
[[email protected] ~]# go run helloworld.go
At times, we’ll need to build our programs into binaries. We can use build command for that.
[[email protected] ~]# go build helloworld.go
[[email protected] ~]# ls
We can then execute the built binary directly like this.
[[email protected] ~]# ./helloworld
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!