Install and Use Golang on Centos 7

In this article, we want to teach you How To Install and Use Golang on Centos 7.

Go, also known as Golang, is an open-source, compiled, and statically typed programming language designed by Google.

It is built to be simple, high-performing, readable, and efficient.

How To Install and Use Golang on Centos 7

Before you start to install Golang on Centos 7, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Centos 7.

Now you can follow the steps below to install Golang on Centos 7.

Set up Golang on Centos 7

Here we will install Golang with the Golang installer. First, update your local package index with the following command:

sudo yum update -y

Then, use the wget command to download the Golang installer on Centos 7:

sudo wget https://storage.googleapis.com/golang/getgo/installer_linux

Now you need to make your downloaded file executable with the following command:

sudo chmod +x ./installer_linux

Next, run the Golang installer to install Golang in the latest version:

./installer_linux 

When your installation is completed, you need to source the ~/.bash_profile:

source ~/.bash_profile

Verify your Golang installation by checking its version:

go version

In your output you will see:

Output
go version go1.17.5 linux/amd64

How To Use Golang on Centos 7

At this point, that you have the Golang installed on your Centos 7, we will show you how to use Golang by setting up a simple workspace.

First, you need to create a workspace directory with the command below:

sudo mkdir ~/go

Then, create a Golang file for your hello world program:

sudo mkdir -p ~/go/src/hello

Now you need to create and open the hello.go file with your favorite text editor, here we use vi:

sudo vi ~/go/src/hello/hello.go

Add the following contents to the file:

package main 
import "fmt" 
func main() { 
  fmt.Printf("Hello, World! Golang is Amazing!\n") 
}

When you are done, save and close the file.

Switch to your hello world directory with the command below:

sudo cd ~/go/src/hello

Next, create the hello module and build the Go file on Centos 7 with the following commands:

$ go mod init 
$ go build

Now run your program with the command below:

./hello

In your output you will see:

Output
Hello, World! Golang is Amazing!

Its means that your Golang installation is working correctly.

Conclusion

At this point, you learn to install Golang on Centos 7.

Hope you enjoy using it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!