How to Install Go Lang on CentOS 8

Go, often referred to as golang is a modern open-source programming language created by Google that allows you to build reliable and efficient applications.

Many popular applications, such as Kubernetes, Docker, Prometheus, and Terraform, are written in Go.

This tutorial explains how to download and install Go on a CentOS 8 system.

Download and install Go on CentOS 8

At the time of writing this article, the latest stable version of Go is version 1.13.4. Before downloading the tarball, visit the official Go downloads page and check if there is a new version available.

Perform the following steps below to download and install Go on CentOS 8:

Step 1:

Download the Go binary using either the wget or curl utility:

wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz

Step 2:

Once the archive is downloaded, verify the tarball checksum by typing:

sha256sum go1.13.4.linux-amd64.tar.gz

Make sure the hash printed from the sha256sum command matches the one from the downloads page.

Output

692d17071736f74be04a72a06dab9cac1cd759377bd85316e52b2227604c004c  go1.13.4.linux-amd64.tar.gz

Step 3:

Extract the tarball to the /usr/local directory using the tar command:

sudo tar -C /usr/local -xf go1.13.4.linux-amd64.tar.gz

The command above must be run as root or a user with sudo privileges.

Step 4:

Tell the system where to find the Go executable binaries by adjusting the $PATH environment variable.

You can do this by adding the following line to the /etc/profile file (for a system-wide installation) or to the $HOME/.bash_profile file (for a current user installation):

export PATH=$PATH:/usr/local/go/bin

Save the file, and load the new PATH environment variable into the current shell session using the source command:

source ~/.bash_profile

That’s it. At this point, Go has been installed on your CentOS system.

Test the Installation

To test whether Go is installed correctly, we will set up a workspace and build a simple “Hello world” program.

Step 1:

The location of the workspace directory is specified with the GOPATH environment variable. By default, it is set to $HOME/go. To create the directory run the following command:

mkdir ~/go

Step 2:

Inside the workspace create a new directory src/hello:

mkdir -p ~/go/src/hello

In that directory create a file named hello.go:

nano ~/go/src/hello/hello.go

Paste the following code to the file:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, World\n")
}

Step 3:

Navigate to the ~/go/src/hello directory and run go build to build the code:

cd ~/go/src/hello
go build

The command above will build an executable named hello.

Step 4:

Run the executable by typing:

./hello

If you see the following output, then you have successfully installed Go.

Output

Hello, World

Conclusion

Now that you have downloaded and installed Go, you can start writing your Go code.

Leave a Reply

Your email address will not be published. Required fields are marked *