Getting started with Go in Ubuntu (linux)
Golang is a programming language introduced by Google in 2009. The reason behind its development is due to frustration at Google, since developers continually had to pick a language that executed efficiently but took a long time to compile, other languages that ran inefficiently in production, but easy to program. Hence, Golang was designed for fast compilation, ease of programming and efficient execution in production.
Go can be used for many different programming projects since it is a versatile programming language. It is well suited for networking/distributed systems programs and widely used in the cloud environment since developers can do more with a strong set of tools and deployment becomes easy since only a single binary must be compiled. Furthermore, Go is easy to learn due to its small set of keywords, which also make a good choice for beginners and experienced developers.
Installation
Installing Golang on a machine running Ubuntu is a straight forward process since it only required some terminal command, a few modifications and you are good to go. In this article, we are going to demonstrate the Golang installation process and start using Go by writing a simple Go application.
Download the latest binary
Before proceeding with this process check the latest Go version on their official website (https://golang.org/dl/). At the time of writing this article, version 1.13.4 was the latest Go version.
For downloading the golang on Ubuntu we will be using curl
instead of wget
.
The following command downloads the latest stable version of Golang:
curl -O https://storage.googleapis.com/golang/go[version].linux-amd64.tar.gz
Open terminal in Ubuntu and enter the following command:
curl -O https://storage.googleapis.com/golang/go1.13.4.linux-amd64.tar.gz
After downloading the binary (.tar) you need to verify it using sha256sum. The sha256sum program is designed to verify data integrity using SHA-256.
When this cryptographic hash algorithm is used properly it can confirm both the file integrity and authenticity.
The following command verifies the .tar file using sha256sum:
sha256sum go[version].linux-amd64.tar.gz
After verifying the file you need to extract the tarball (https://www.computerhope.com/jargon/t/tarball.htm) by executing the following command:
tar -xvf go[version].linux-amd64.tar.gz
After extracting the tarball you need to adjust the permission and move to the go
directory which is located in usr/local
.
To adjust the tarball permission enter the following command in terminal:
sudo chown -R root:root ./go
Enter your password and press ‘enter’.
After adjusting the tarball permission, move to the go directory by entering the following command:
sudo mv go /usr/local
Set your path variable
In order to set the path variable, you must navigate to Home directory by enter the following command: cd ~
.
To view all the files/directory in the Home directory enter the following command:
ls -la
(if everything went well you must see .profile
appearing in the list).
Open the .profile
in TextEditor
by entering the following command:
gedit .profile
Add the following two lines to the bottom of the file:
Test your Go installation
As you already set your variable path to HOME -> Go
navigate to the home directory by entering the following command:
cd ~
.
Ones you are in the home directory, create the Go
directory through the following command:
mkdir go
If the process went well you should see your go directory in your home directory.
In this example, we are going to create a hello world Go app.
Navigate to the Home directory and create the src
and helloworld
folder and navigate to the helloworld
directory through the following command:
mkdir -p go/src/helloworld && cd go/src/helloworld
Next, you need to create the .go
file for this sample project.
Type the following command to create the golang file: touch helloworld.go
.
Navigate to go/src/helloworld
and you must be able to see the .go
file.
Open the .go file in TextEditor by entering the following command in terminal:
gedit helloworld.go
In this example, we just going to print ‘hello, world’ in the terminal console.
Type the following code and save the file:
For Ubuntu version 18+ users
In case you try to execute your Go code and you get the following message:
Just execute these simple commands that appear in the message:
Sudo apt install golang-go
Enter y
and press enter
Afterward, install gccgo-go
by entering the following command:
sudo apt install gccgo-go
Enter y
and press enter
To run the program just type the following command in terminal:
go run helloworld.go
Relevant resources:
Follow us: