CentOS

Install Go (Golang) on CentOS 7 / RHEL 7

Go or GoLang, as it is called, is an open-source programming language designed to help ease the development of simple and scalable applications. It was implemented as a product of Google Engineering in 2009. It was referred to as a statically-typed programming language with the ease of use, versatility, and reliability.

Original content from computingforgeeks.com - post 109932

Currently, Golang has become one of the most popular programming languages due to its efficiency, clean design, expressive, and concise design model. Some of the projects that make use of Golang are:

  • snappy – a package manager
  • Juju – a service orchestration tool by canonical
  • Docker – a set of tools for deploying containers
  • Dropbox moved the critical components to Go lang from python

The features of the Go programming language are:

  • It is Open-Source
  • It has a powerful Standard Library and Tool Set
  • Concurrency Support – it offers easy and trackable concurrency options.
  • Garbage Collection
  • Testing Capabilities
  • Cross-Platform Development Opportunities
  • Hiighly scalable

This guide offers a systematic demonstration of how to install Go (Golang) on CentOS 7 / RHEL 7.

Step 1 – Update System

First, ensure that your system is up-to-date and packages are to their latest versions. This is achieved using the command below.

sudo yum update

Install the required packages.

sudo yum install wget

Step 2 – Install Go (Golang) on CentOS 7 / RHEL 7

This installation method requires one to download the latest available Golang package from the official Golang release page.

Alternatively, you can download the tarball using wget as below.

wget https://go.dev/dl/go1.21.1.linux-amd64.tar.gz

With the tarball downloaded, verify the checksum.

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

Sample Output:

b3075ae1ce5dab85f89bc7905d1632de23ca196bd8336afd93fa97434cfa55ae  go1.21.1.linux-amd64.tar.gz

Verify that the output matches the one on the download page.

Next, extract the tarball.

sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz

Then proceed and adjust your PATH variable. This will tell your system where to find and execute the GO executable binaries. This can be done by adding the below line to $HOME/.profile for installation on the current user or /etc/profile for the system-wide installation.

##For Current user
$ vi $HOME/.profile 

##For system-wide
$ sudo vi /etc/profile 

In the file, add the below lines.

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

For the changes made to apply, you may be required to log out and log in back to your system or directly execute them from the profile with the command:

source $HOME/.profile
##OR##
source /etc/profile

That it! You have installed Golang on your CentOS 7 / RHEL 7 system.

Now, verify your installation by checking the installed Golang version.

$ go version
go version go1.21.1 linux/amd64

Step 3 – Test your Go Installation on CentOS 7 / RHEL 7

Here, we want to test our Go installation by setting up a simple workspace then building a simple Hello World program using Golang.

First, create a workspace directory.

mkdir ~/go

Then create a Go file for the Hello World program.

mkdir -p ~/go/src/hello

Now in the directory, create a hello.go file as below.

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

In the created file, add the below lines.

package main

import "fmt"

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

Build the Go file.

cd ~/go/src/hello 
go mod init
go build

Now execute your program.

./hello

Sample Output:

rhellab$ ./hello
Hello, World! Golang is Amazing!

From the output above, it is safe to assume that our Golang installation is working perfectly.

Conclusion

That was enough learning! I hope you too managed to install Go (Golang) on CentOS 7 / RHEL 7.

Interested in more?

Related Articles

CentOS Install Tomcat 9 on CentOS 8|RHEL 8|Rocky Linux 8 Apache How to Setup ISPConfig DNS Only on CentOS 7 CentOS Install Fathom analytics on Ubuntu/Debian/CentOS CentOS Set system hostname on CentOS / RHEL / Rocky / Alma

Leave a Comment

Press ESC to close