Cyberithub

Easy Steps to Install GO Using YUM on CentOS 7

In this article, I will take you through easy steps to Install GO Using YUM on CentOS 7. Go isn’t just an open source language, it is a language embraced and supported by a community of people who thrive on open source and all that it provides. Go isn’t the typical statically typed and compiled language. The static typing has features that make it feel dynamic, and the compiled binaries have a runtime that includes garbage collection. The design of the language took into account the types of projects that Google would need to use it for: large codebases operating at scale and being developed by large developer teams.

At its core, Go is a programming language defined by a specification that can be implemented by any compiler. The default implementation is shipped via the go tool. But Go is more than a programming language.

 

Easy Steps to Install GO Using YUM on CentOS 7 1

Install GO Using YUM

Also Read: Easy Steps to Install GO Using APT GET on Ubuntu 18.04

Step 1: Prerequisites

a)You require a running CentOS 7 System having root access or User having sudo access to run all privilege command. Here I am running all the commands from root access to make things simple and straight. To provide sudo access to user, you can check How to add User to Sudoers.

b)Before going through the steps to Install GO package, you can install EPEL package by using yum install epel-release command as shown below.

[root@localhost ~]# yum install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Step 2: Update Your System

Before installing GO in your system, you need to update all the packages to the latest version using yum update command as shown below.

[root@localhost ~]# yum update
Resolving Dependencies
--> Running transaction check
---> Package NetworkManager.x86_64 1:1.18.0-5.el7_7.1 will be updated
---> Package NetworkManager.x86_64 1:1.18.0-5.el7_7.2 will be an update
---> Package NetworkManager-libnm.x86_64 1:1.18.0-5.el7_7.1 will be updated
---> Package NetworkManager-libnm.x86_64 1:1.18.0-5.el7_7.2 will be an update
---> Package NetworkManager-team.x86_64 1:1.18.0-5.el7_7.1 will be updated
---> Package NetworkManager-team.x86_64 1:1.18.0-5.el7_7.2 will be an update
---> Package NetworkManager-tui.x86_64 1:1.18.0-5.el7_7.1 will be updated

Step 3: Install GO Using YUM

Now, you can install GO package using yum install golang command.

[root@localhost ~]# yum install golang
(1/3): epel/x86_64/group_gz | 90 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/3): epel/x86_64/primary_db | 6.7 MB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package golang.x86_64 0:1.13.6-1.el7 will be installed
--> Processing Dependency: golang-bin = 1.13.6-1.el7 for package: golang-1.13.6-1.el7.x86_64
--> Processing Dependency: golang-src = 1.13.6-1.el7 for package: golang-1.13.6-1.el7.x86_64
--> Running transaction check
---> Package golang-bin.x86_64 0:1.13.6-1.el7 will be installed

Step 4: Check GO Version

After successful installation, you can check GO version by running go version command.

[root@localhost ~]# go version
go version go1.13.6 linux/amd64

Step 5: Write Your First GO Program

Now that GO is installed successfully, it is time to write your first "Hello World" GO Program as shown below. Since this is a simple program so we added package main at the top of the program which will tell GO compiler to compile this as an executable program. It also define the entry function of GO Program.

We have also used import keyword to import "fmt" package from Standard GO Library which provides printf function to display the output.

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

Step 6: Build Your Program

Now you can build your hello.go program by running go build command. Please note that i have created a directory hello and written my hello.go program under that.

root@localhost:~/hello# go build

Here you can see a file hello is created after a successful build.

root@localhost:~/hello# ls -lrt
total 1980
-rw-r--r-- 1 root root 73 Feb 22 15:19 hello.go
-rwxr-xr-x 1 root root 2020012 Feb 22 15:55 hello

Step 7: Run Your Program

Now you can run your program by executing ./hello. You can see hello, world at the output of the program.

root@localhost:~/hello# ./hello
hello, world

Step 8: Alternative Way to Run Your Program

There is another shortcut way to run your program without explicitly building it by using go build command. You can directly run go run hello.go command which compiles and execute your program as shown below.

root@localhost:~/hello# go run hello.go
hello, world

As you can see from below output, there is no separate hello executable got created which was the case in Step 6.

root@localhost:~/hello# ls -lrt
total 4
-rw-r--r-- 1 root root 73 Feb 22 15:19 hello.go

 

Reference: GO in Practice

Leave a Comment