paint-brush
How to Install Go on Linux - Tutorialby@Bairesdev
259 reads

How to Install Go on Linux - Tutorial

by BairesDevJune 24th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to Install Go on Linux - Tutorial with Bairesdev BairesDev. The Go programming language has become quite popular over the past few years. Go includes the disciplined syntax of C, but adds a number of changes that do away with memory leakage. It’s fast, easy to learn, scales incredibly well, is lean, and efficient. Go can be installed on nearly any Linux distribution. The only thing you'll need to get it installed is a running instance of Linux. You'll be surprised at how easy the installation is.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Install Go on Linux - Tutorial
BairesDev HackerNoon profile picture

The Go programming language has become quite popular over the past few years. Why? The primary reason is that it has become the language of choice for distributed and highly scalable servers. Go was based on C and brings code efficiency to the table, which generates faster software and apps for businesses. 


Go includes the disciplined syntax of C, but adds a number of changes that do away with memory leakage. It’s fast, easy to learn, scales incredibly well, is lean, and efficient. Many companies have quickly adopted this language. Some of the more notable businesses that have done so include:

  • Google
  • Apple 
  • Facebook
  • Docker
  • The New York Times
  • BBC

Chances are your favorite QA testing services are either already up to speed with Go, or in the process of adopting this powerful language.

Let's get Go installed on the Linux operating system and start using the language. You'll be surprised at how easy the installation is.

Installing Go on Linux

I'll be demonstrating on Ubuntu Server 20.04, but Go can be installed on nearly any Linux distribution. Go is also available for macOS and Windows. The only thing you'll need to get it installed is a running instance of Linux.

The first thing to be done is the downloading of the necessary file. Change into your Downloads directory with the command:

cd ~/Downloads

Now you can download the file with the command:

curl -O https://storage.googleapis.com/golang/go1.13.5.linux-amd64.tar.gz 

With that file downloaded, unpack it with the command:

tar -xvf go*.tar.gz

Next, move the newly-created Go folder with the command:

sudo mv go /usr/local

The next step is to add the go folder to your user's

$PATH
. For those that don't know what a user's
$PATH
is, it's any directory that is considered global. A global directory means any executable file within that directory can be run from anywhere in the file system.

To see the current directories in your

$PATH
, issue the command:

echo $PATH

You will see that the newly moved Go directory isn’t included. To add that directory to your $PATH, issue the command:

nano ~/.profile

Scroll to the bottom of that file and add the following lines:

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

Save and close that file. 

Refresh your profile with the command:

source ~/.profile

You can now check to make sure the Go folder is in your $PATH with the command:

go version

The above command should print the version number of the installed Go language.

And that’s all there is to installing the Go language on Linux.

Hello, World!

I'm going to use the traditional "Hello, World!" example to demonstrate how Go works. Yes, this is a very simple and basic program, but it's a great way to illustrate how to use a new language. 

The first step is to create the hello-world.go file. Issue the command:

nano hello-world.go

In this new file, paste the following contents:

package main


import "fmt"


func main() {
    fmt.Println("Hello, World!")
}

Save and close the file by using the [Ctrl]+[x] key combination.

One of the interesting aspects of Go is that you can run a program without compiling it. To do that, you would issue the command:

go run hello-world.go

The results of the command would print out:

Hello, World!

You can also compile a go program, which would create an executable binary. To build the binary file from the hello-world.go file, issue the command:

go build hello-world.go

The results of the build would produce a new file named hello-world. You could then run that file with the command:

./hello-world

You should see the same message printed out as you did when you ran the uncompiled file. 

Of course, you could always copy that file to a directory in your path, so the new application could be run from anywhere in the directory structure. To do that, issue the command:

sudo mv hello-world /usr/local/bin

Now, to run the hello-world application, just issue the command:

hello-world

You should then see the "Hello, World!" message printed out. 

Conclusion

As a developer, you'll wind up having to work with Go at some point. Whether you're joining a project as a new software engineer, joining a web application testing firm, or starting a new job, Go will cross your path. At least now, you can install the necessary tools and have an idea of how to create and build an app with Go on Linux.

To find out more go to BairesDev.