Go, install this programming language on Ubuntu 20.04

about Go installation on Ubuntu 20.04

In the next article we are going to take a look at how we can install Go, often referred to as golang, on Ubuntu 20.04. This is a modern open source programming language created by Google that will allow us to create reliable and efficient applications. It is a compiled language, which means that you will need to compile the source code to create an executable file that is used to run the software.

Many popular applications, such as Kubernetes, Docker, Prometheus and Terraform, are written in Go. This is a compiled, concurrent, imperative, structured, non-object-oriented programming language with garbage collector.

Some of the general features of Go

  • It is a one project open source.
  • This language use a syntax similar to C.
  • Use static typing and its performance is comparable to that of languages ​​like C and C ++ since, like these, compiler converts Go code to machine code.
  • This language has many of the features and facilities of dynamic languages like python.
  • Although it is a language designed for system programming, provides a garbage collector and other high-level capabilities that make it a very powerful language.
  • The Go binary has the characteristic of cross compilation natively.
  • Go supports the object-oriented programming paradigm, but unlike most popular programming languages, it does not have type and keyword inheritance.
  • This is a programming language designed to take advantage of multi-processor systems.

Install Go on Ubuntu 20.04

To install Go in Ubuntu 20.04 we will only have to follow the following steps:

Download the Go tarball

As of this writing, the latest stable version is 1.14.2. Before downloading the tarball, it is advisable to visit the official download page Go and check if a new version is available.

To download the package that interests us, we can do it from web browser or using wget in a terminal (Ctrl + Alt + T):

download go tarbal pack

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

After downloading, we will unzip the downloaded file in the directory / usr / local:

sudo tar -xvf go1.14.2.linux-amd64.tar.gz -C /usr/local/

Fit from path variable

Al add the Go directory location to the $ PATH environment variable, the system will know where to find the Go executable binaries.

This can be done by adding the following line to the file / Etc / profile (for a system-wide installation) or to the file $ HOME / .profile (for a current user installation):

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

Once the previous line has been added to the file that interests us the most, it only remains to save it and load new PATH environment variable in current shell session with the command:

source ~/.profile

Verify the installation

We will be able see the version installed on the system typing in terminal (Ctrl + Alt + T):

version of Go installed

go version

A little example

To test the installation of this language we are going to create a simple program that prints the classic message of 'Hello World'.

By default the GOPATH variable, which specifies the location of the workspace, is set to $ HOME / go. To create the workspace directory we only need to write in the terminal (Ctrl + Alt + T):

mkdir ~/go

Within the workspace, we will create a new directory called src and inside the directory Hello:

mkdir -p ~/go/src/hola

In this directory, using our favorite editor we will create a file called hello.go, and inside we are going to paste the following code:

example file with Go

package main

import "fmt"

func main() {
         fmt.Printf("Hola, esto es una prueba de go en Ubuntu 20.04\n")
}

Once pasted, we will only have to save the file and exit. To get more on the Go Workspace directory hierarchy, users can visit the Documentation page.

Now we are going to navigate to the directory ~ / go / src / hello y run the following command to compile the program:

compilation of the example with Go

cd ~/go/src/hola

go build

The above command will create an executable file called hello. We can execute this by typing the command:

running the sample file

./hola

Now that we have Go downloaded and installed on our Ubuntu 20.04 system, we can start developing our projects. For more information about this language, users can consult the official documentation.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.