Go is a very new and most popular open-source programming language; Google creates it. There are many popular software/applications developed using the Go language, including Grafana, Docker, Kubernetes, and many more.
In this tutorial, we will go through the process of setting the Go language environment in your Ubuntu 20.04 Linux from download to install step-by-step.
Prerequisites
Before starting the installation process, Go, make sure your system runs on Ubuntu 20.04 Linux, and has user access with sudo privileges to execute administrative commands.
Installing Go on Ubuntu 20.04 Linux
The Go language installation and configuration in Ubuntu Linux is very easy; you can download and install Go into Ubuntu Linux in the few steps.
Follow the below steps to install Go on Ubuntu 20.04:
Step 1 – Downloading the Go tarball package.
Currently, the latest version of Go Language is 1.15.2 available; before downloading the tarball package, you can visit the official downloading page of Go and check available new version available.
You can download the Go binary, using either curl or wget command:
$ wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
Step 2 – Verify the Go tarball package.
You can verify the tarball checksum using the sha256sum command, as shown below:
$ sha256sum go1.15.2.linux-amd64.tar.gz
You will get the output similar like below:
output:
68a2297eb099d1a76097905a2ce334e3155004ec08cdea85f24527be3c48e856 go1.15.2.linux-amd64.tar.gz
You should make sure the printed hash from the above command matches one from the download package mention on the download page.
Step 3 – Extract Go package.
You can use the tar command to extract the tarball package to the /usr/local directory, as shown below:
$ sudo tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz
Step 4 – Configure the Path Variable
The Ubuntu operating system needs to manage Path for all executable programs to find the program’s executable binaries.
To configure Go executable binaries, PATH needs to adjust the $PATH environment variable. You can do this by appending the below line in the /etc/profile file:
export PATH=$PATH:/usr/local/go/bin
After adding the line, save the file and load the new PATH environment variable into the same shell session using the following command:
$ source ~/.profile
Step 5 – Verify the Go Installation
You can verify the Go programming language installation on the Ubuntu system by checking the version as shown below:
$ go version
You will get the output similar like below:
output:
go version go1.15.2 linux/amd64
Go Language basics
In Go Language’s basic uses, we will set up a workspace and create a simple program, “program1,” that will print the message “hello world.”
To create a Go program, we will follow these four steps:
Step 1 – Create a Go workspace.
By default, we creating workspace directory is in the user’s home directory $HOME/go. To create it, use the following command:
$ mkdir ~/go
Step 2 – Create a program directory under the workspace.
It would be best if you created a new directory src/program1 inside the workspace, as shown below:
mkdir -p ~/go/src/program1
In the newly-created directory (program1), create a file named hello.go with the following go program:
package main import "fmt" func main() { fmt.Printf("Hello, World\n") }
If you want to learn more about the Go workspace directory hierarchy, please check this link.
Step 3 – Build a Go program.
To build the go application for the program, navigate to the ~/go/src/hello directory, and make a file using the go build command, as shown below:
$ cd ~/go/src/hello
$ go build
The above command will build and an executable file named program1.
Step 4 – Run the new Go program.
You can execute the new build program by running the command, as shown below:
./hello
You will get the output similar like below:
output:
Hello, World
Conclusion
Now you learn to download and install Go language in Ubuntu 20.04 Linux and learn to build workspace, write and run Go programs.
If you get any problem or having any feedback, please comment below.
0 Comments