Tips on how to Set up Go Programming Language on Linux (Ubuntu/Debian/CentOS)

Tips on how to Set up Go Programming Language on Linux (Ubuntu/Debian/CentOS)

All

Linux Articles / All 21 Views comments

Go or Golang, is an open-source programming language designed to be quick, straightforward, environment friendly and scalable. It's a trendy programming language developed by Google that helps you develop easy, dependable and environment friendly software program. There are some widespread DevOps instruments constructed on Go, together with Docker, Kubernetes, Terraform, Grafana, Hoshicorp Vault, and so forth.

On this tutorial, we'll present you learn how to set up Go on a Linux System. With this information, you possibly can set up Go on numerous Linux techniques, together with Debian 10, Ubuntu 18.04 and CentOS 7.

What we'll do:

  • Obtain Go Newest Secure Model
  • Configure System Surroundings for Go
  • Testing - Hey World

Step 1 - Obtain Go Newest Secure Model

Obtain the newest Go model '1.13' utilizing the wget command under.

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

As soon as it is full, confirm the downloaded tarball model utilizing the command under.

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

And you'll get the 'sha256' checksum hashes of the file as under.

68a2297eb099d1a76097905a2ce334e3155004ec08cdea85f24527be3c48e856  go1.13.linux-amd64.tar.gz

Subsequent, extract the Go tarball file 'go1.13.linux-amd64.tar.gz' to the '/usr/native' listing.

tar -C /usr/native -xzf go1.13.linux-amd64.tar.gz

Now the Go binary package deal has been put in to the Linux system underneath the '/usr/native/go' listing.

Unpack Golang archive

Step 2 - Configure System Setting for Go

After downloading and set up Go binary package deal, we have to arrange the system surroundings on the Linux system. We will edit the 'profile' on your shell. For bash consumer, you'll be able to edit the '.bash_profile' and for zsh shell customers is the '.zprofile'.

For Debian and Ubuntu Consumer

For the debian consumer, you will have to edit/create the '~/.bash_profile' configuration on the house listing.

Go to the consumer house listing and edit/create the configuration '~/.bash_profile' utilizing vim editor.

vim ~/.bash_profile

Now paste the surroundings configuration for Go lang under.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/native/go/bin:$GOPATH/bin

Save and shut.

After that, we have to load the '~/.bash_profile' on each login. It is advisable to load it via the '~/.bashrc' configuration.

Edit the '~/.bashrc' file utilizing vim editor.

vim ~/.bashrc

Add the configuration under to the underside of the road.

supply ~/.bash_profile

Save and shut.

Configure system environment

Now sign off out of your shell and log in once more, and the surroundings variable for Go has been configured.

For CentOS 7 Customers

For the CentOS 7 consumer, you simply have to edit the '~/.bash_profile' file and cargo it manually.

vim ~/.bash_profile

Paste the next variables.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/native/go/bin:$GOPATH/bin

Save and shut.

Now load it manually utilizing the 'supply' command as under.

supply ~/.bash_profile

Now the surroundings variables for Go has been configured.

Edit user profile

For ZSH Customers

For zsh customers, you'll need so as to add the system setting configuration to the '~/.zprofile' configuration.

Go to the consumer house listing and create/edit the configuration file '~/.zprofile' utilizing vim editor.

vim ~/.zprofile

Paste the next configuration

export GOPATH=$HOME/work
export PATH=$PATH:/usr/native/go/bin:$GOPATH/bin

Save and shut.

And the '~/.zprofile' might be loaded mechanically at login. To make an impact, sign off out of your shell and log in once more.

Create the Working Listing

Subsequent, after configuring the surroundings variable for the Golang, we have to create the Go working listing beneath the house consumer listing.

mkdir -p $HOME/work

After that, verify the surroundings that we have created utilizing the next instructions.

echo $GOPATH
echo $PATH

Now you'll get the '$GOPATH' because the Go working listing and the Go binary path has been added to the '$PATH' setting.

And it is possible for you to to run the go instructions under.

go model
go --help

In consequence, Golang surroundings configuration on the Linux system has been accomplished efficiently.

Check go version

Moreover: Set Go Surroundings Variable System-wide

To setup Go setting variable globally for all customers that utilizing the Bash shell on the system, you'll need to outline the surroundings variable underneath the '/and so on/profile.d' listing. And it is the identical for all main Linux methods that used bash as default shell reminiscent of Debian, Ubuntu CentOS.

Now Go to the '/and so forth/profile.d' listing and create a brand new configuration file 'go.sh' utilizing vim editor.

cd /and so on/profile.d/
vim go.sh

Paste the Go surroundings variable as under.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/native/go/bin:$GOPATH/bin

Save and shut, then make the file executable.

chmod +x /and so forth/profile.d/go.sh

And all customers contained in the system that used Bash as its shell will have the ability to run go by default.

Set Go Environment Variable System-wide

Step three - Testing - Good day Go

On this step, we're to check our Go set up by creating the straightforward 'Hiya World' program. And the '$GOPATH' would be the main working listing.

Go to the '$GOPATH' working listing, create new 'src' and 'bin' listing on it.

cd $GOPATH
mkdir -p src/ bin/

Now create a brand new 'hiya' challenge listing underneath the 'src' and go into it.

mkdir -p src/hey/
cd src/howdy/

Create the Golang file 'good day.go' utilizing vim editor.

vim whats up.go

Paste the straightforward Go script under.

package deal major

import (
    "fmt"
)

func principal()
    fmt.Println("Hiya World, Go")

Save and shut.

Subsequent, compile the 'hey.go' program to the '$GOPATH/bin' listing utilizing the next command.

go construct -o $GOPATH/bin/howdy hiya.go

Now the compiled program referred to as 'hey' might be out there on the '$GOPATH/bin' listing, and you may simply run the 'hiya' command because it has been outlined on the binary '$PATH' listing.

good day

And you'll get the outcome as under.

Hello World Go program

Consequently, the Go set up in your Linux system has been accomplished efficiently.

Reference

Comments