Go, install this programming language on Ubuntu 17.10

about go

In the next article we are going to take a look at Go. Also known as GoLang, it is an open source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. The Go language is available in the default repositories of most Gnu / Linux distributions. This language is being actively used on some of Google's production servers, as well as other companies such as Dropbox, Soundcloud, Uber, etc.

In this article we will see how to install and configure the Go programming language in Ubuntu. This is a programming language compiled, competitor, imperative, structured, not object oriented and with garbage collector. We will be able to install Golang using the package manager or downloading the code.

Install Go on Ubuntu

Install the Go language using package managers

In DEB-based systems like Debian,Ubuntu, Linux Mint, we can install it using the following command in the terminal (Ctrl + Alt + T):

sudo apt-get install golang

You can also search for e install additional tools. On DEB-based systems, run in the same terminal:

sudo apt-cache search golang

Install Go from source

The Go language version in the default repositories may be out of date. If you are interested in installing the latest version, download it from the official website and install it manually as described below. For this example, this has been the option I have followed.

The following commands I have tested in the version of Ubuntu 17.10. However, these steps are the same for other Gnu / Linux distributions. Download the latest compressed version from your web page.

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

Now check the integrity of the file downloaded as shown below.

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

The value of SHA256 checksum that the previous command shows you must match the one provided with the download link. If it doesn't match, download it again.

Extract the downloaded file with the command:

sudo tar -C /usr/local -xvzf go1.9.3.linux-amd64.tar.gz

It will be extracted into in the directory / usr / local. The -C flag indicates the destination directory.

Configure Go

Now, we must set the path to Go in the user's profile. Edit your user profile by typing in the terminal (Ctrl + Alt + T):

sudo vi ~/.profile

Add the following line:

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

Save and close the file.

Now we will configure the workspace. A workspace is a hierarchy of directories. In this case we are going to give you three directories in its root:

  • src
  • pkg
  • bin

We can create this hierarchy of directories using the following command in the terminal:

mkdir -p $HOME/go_projects/{src,pkg,bin}

Next, we must point to the new workspace. To do this, edit the ~ / .profile file:

sudo vi ~/.profile

And in it add the following lines:

export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"

Go user profile configuration

If Go is installed in a location other than the default location (/ usr / local /), you must specify the installation path (GOROOT) in the ~ / .profile file. For example, if you have installed Go in your HOME directory, then you will have to add the following lines in the user profile file:

export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

Note that if you have installed Golang using package managers, the installation path will be / usr / lib / go o / usr / lib / golang. In this case you will need to update the value of the route in GOROOT.

Once you've specified the appropriate values, update the Go environment values ​​by typing:

source ~/.profile

Verify the installation

go information and installation check

Run the following commands to check if everything has been installed and configured correctly. Let's see the version installed with:

go version

Para ver the environment information, run:

go env

If you see results like the ones in the previous screenshot, congratulations! you can start programming now.

Create a 'hello world' using Go

Now that we have Go installed, let's go ahead and believe a simple 'hello world' program.

We are going to create a file called hello.go with the following command:

vi go_projects/src/hola/hola.go

Add the following lines in it:

package main

import "fmt"

func main() {
fmt.Println("Hola usuarios de Ubunlog. Este es un pequeño programa utilizando Go en Ubuntu 17.10")
}

Save and close the file. Launch the following command to compile the program:

go install $GOPATH/src/hola/hola.go

Finally, run the program using command:

installation and launch of the program in Go

$GOBIN/hello

If everything went well, congratulations! You just created an example program with Go.

For more details, see the help section running:

Go Help

go help

You can also check the official documentation by Go.

In case you no longer want this language, you can uninstall it Through the package manager or simply deleting the / usr / local / go directory. It will depend on the installation option you take. In addition, it also removes the directories from the workspace.


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.