How To Install Go (Golang) 1.7 on CentOS 7


In this article, we will learn about how to install and configure Go (golang) which is developed by Google and its open source programming language. It’s a simple, efficient and reliable programming language for development with minimalist.

Prerequisites

  • A CentOS machine installed.
  • A non-root user with Sudo permission on the CentOS machine.

Downloading and Installing the GO

The Go (golang) is not up to date on the CentOS repository, so we will manually download and install the package directly from the Go lang website and also make sure that we have the latest version which is compatible with our system architecture.

Let’s move to the writable and temporary directory where we can download the package from the Go website and install.

$ cd /tmp

We will use the curl command to download the Go with the below link

$ curl -LO https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
84 77.7M 84 65.5M 0 0 493k 0 0:02:41 0:02:15 0:00:26 0
curl: (56) TCP connection reset by peer

Once the package is downloaded from the site, we will extract the package to /usr/local and install the package.

Below is the command to extract the package to /usr/local

$ tar -C /usr/local/ -xvf go1.7.linux-amd64.tar.gz

Once the packages are extracted in /usr/local we needed to create a workspace with three sub-directories, we consider the parent directory as ~/myproject, below is the command to create the three sub-directories.

$ mkdir –p ~/myproject/{bin,pkg,src}

Setting Up The Environment Variables for the Go

If we want to execute the Go like the other command we needed to all the paths to the $PATH variables for that we needed to create a file path.sh to /etc/profile.d folder using any text editor

$ sudo nano /etc/profiled/path.sh
Export PATH=$PATH:/usr/local/go/bin

We have to define the GOPATH and GOBIN which are GO environment variable on the .bash_profile for pointing the workspace. Where are GOPATH will show the location of the source files and GOBIN will stores the binary files which are created and is compiled.

Edit the .bash_profile with any of your choice editor using the below command

$ nano ~/.bash_profile
export GOBIN="$HOME/myprojects/bin"
export GOPATH="$HOME/myprojects/src"

To apply the changes made in the .bash_profile we needed to run the below command

$ source /etc/profile && source ~/.bash_profile

Creating a Simple Go Program

As the Go (golang) environment is ready, we needed to test our environment, we will write one simple Go program.

$ nano ~/myprojects/src/welcome.go

Below is the code which will print Welcome to the Go (golang) .

Package main
Import “fmt”
   Func main()
      {
         Fmt.Printf(“Welcome to the Go (golang) \n “)
      }

As we have written a simple code for testing the Go environment, we have to compile the ‘welcome.go’ with go install command, bellow is the full command to compile the file.

$ go install $GOPATH/welcome.go

Once the program is compiled, we can test the program with the below command –

$ $GOBIN/welcome
Welcome to the Go (golang)

In the above article we have learned how to install the Go (golang) programming language which is from Google and we have written a simple program and executed to test the environment to confirm the installation is successful.

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 20-Jan-2020

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements