Table of Contents
In this tutorial, I will take you through the steps to Install Golang on Ubuntu 18.04. It was created by small team in Google which consists of Robert Griesemar, Rob Pike and Ken Thompson. GO is a strong and statically type language.
Strong type means type of a variable cannot change over time. It means if you declare a variable to hold an integer, it will always hold an integer. You cannot put any boolean or string value in it. Static type means all of the variables has to be defined at the compile time.
Why GO Language?
At the time GO was created, we had few powerful languages available at that time i.e Python, Java and C/C++. But over the time, we had certain limitations with these languages. For e.g:-
a)Python is easy to use but it is little slow.
b)Java program has becoming increasingly complex type system.
c)C/C++ also becoming complex over the time and hence compile time is getting slower.
So GO language is created by optimizing the compiler and improving the compile time. You will know more about GOLANG in next few tutorials. Let’s follow the steps to install GOLANG in this tutorial.
Install GOLANG On Ubuntu
Before going through the steps to install golang in your system, you need to ensure prerequisites are met.
Also Read: Networking in Docker with Best Examples
Step 1: Prerequisites
a)You must have a running Ubuntu 18.04 System.
b)You need to login as root user or user with sudo access to run below commands. You can visit How to add User into Sudoers on Ubuntu 18.04 to check the steps to add User into Sudoers.
Step 2: Update Your System
You need to update your system using sudo apt-get update
command.
test@localhost:~$ sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 https://artifacts.elastic.co/packages/7.x/apt stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Reading package lists... Done
Step 3: Download Golang Tar file
You can go to GOLANG Download Page and download tar file for Linux using wget command. This will download go1.13.6.linux-amd64.tar.gz in current directory.
test@localhost:~$ wget https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz --2020-01-20 22:39:29-- https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz Resolving dl.google.com (dl.google.com)... 172.217.31.206, 2404:6800:4007:809::200e Connecting to dl.google.com (dl.google.com)|172.217.31.206|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 120081829 (115M) [application/octet-stream] Saving to: ‘go1.13.6.linux-amd64.tar.gz’ go1.13.6.linux-amd64.tar.gz 100%[=====================================================================================>] 114.52M 5.73MB/s in 15s 2020-01-20 22:39:45 (7.55 MB/s) - ‘go1.13.6.linux-amd64.tar.gz’ saved [120081829/120081829]
Step 4: Extract Tar File
Once golang tar file is downloaded, you need to extract the compressed file by using tar
command as shown below.
test@localhost:~$ tar -xvf go1.13.6.linux-amd64.tar.gz
go/test/fixedbugs/bug072.go
go/test/fixedbugs/bug073.go
go/test/fixedbugs/bug074.go
go/test/fixedbugs/bug075.go
go/test/fixedbugs/bug076.go
go/test/fixedbugs/bug077.go
go/test/fixedbugs/bug078.go
go/test/fixedbugs/bug080.go
go/test/fixedbugs/bug081.go
go/test/fixedbugs/bug082.go
go/test/fixedbugs/bug083.dir/
............................................................................................................................................
Step 5: Setting Up the Directory
Move extracted go/ directory to /usr/local using below command.
test@localhost:~$ sudo mv go/ /usr/local/
Now you need to create a go/ directory in /home/test which will used as Project Workspace. You can choose any name you want for creating workspaces. It really does not matter.
test@localhost:~$ cd test@localhost:~$ mkdir go
Set the Path at the end of ~/.profile. You need to ensure that .profile is required to be set for all those users through which you want to build and run your GO Programs.
test@localhost:~$ nano ~/.profile
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Once profile is edited and saved. You need to load the Profile Again by using below command.
test@localhost:~$ source .profile
Step 6: Check GO Version
Once GO Path is set, you can check the GO Version by running go version
command as shown below.
test@localhost:~$ go version
go version go1.13.6 linux/amd64
Step 7: Write Your First GO Program
Create another directory example under go directory. We will write our first GO Program in this directory.
test@localhost:~/go$ mkdir example
Then, write a simple hello World program in hello.go
as shown below. We will use our favourite editor nano to open the file and write our code.
test@localhost:~/go/example$ nano hello.go package main import "fmt" func main() { fmt.Printf("hello, world\n") }
Step 8: Build Your GO Program
Now build your program with go build
command as shown below.
test@localhost:~/go/example$ go build
If you don’t receive any error after completing the build then you need to check if the executable got created or not by running ls
command. You can see your executable example
got created.
test@localhost:~/go/example$ ls example hello.go
Step 9: Test Program Output
Now you can run your Program and test the output as shown below.
test@localhost:~/go/example$ ./example hello, world
Also Read: Top 15 tools to monitor disk IO Performance with Examples