How to Install Go (Golang) on Linux Mint 19

4287
Share:
how-to-install-go-golang-on-linux-mint-19

Go (also referred to as Golang) is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.

Go was designed at Google in 2007 to improve programming productivity in an era of multicore, networked machines and large codebases. The designers wanted to address criticism of other languages in use at Google, but keep their useful characteristics:

  • Static typing and run-time efficiency (like C++)
  • Readability and usability (like Python or JavaScript)
  • High-performance networking and multiprocessing

The designers were primarily motivated by their shared dislike of C++.

Installation Methods

Just as old saying "all roads lead to Rome", many ways can be used to install Go on Linux Mint. However, I'll just mention two methods here:

  • Install from official Ubuntu repository
  • Install official binary from Golang website

Install from Official Ubuntu Repository

This is the most straightforward method, as you'll just need to type one command:

$ sudo apt install golang

After pressing enter, your terminal will shows something like this:

Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  golang
0 upgraded, 1 newly installed, 0 to remove and 310 not upgraded.
Need to get 2.896 B of archives.
After this operation, 11,3 kB of additional disk space will be used.
Get:1 http://mirror.poliwangi.ac.id/ubuntu bionic/main amd64 golang amd64 2:1.10~4ubuntu1 [2.896 B]
Fetched 2.896 B in 0s (15,9 kB/s) 
Selecting previously unselected package golang.
(Reading database ... 360791 files and directories currently installed.)
Preparing to unpack .../golang_2%3a1.10~4ubuntu1_amd64.deb ...
Unpacking golang (2:1.10~4ubuntu1) ...
Setting up golang (2:1.10~4ubuntu1) ...

Congratulation, you've installed Go (Golang) successfully.

Install Official Binary from Golang Website

If you want to get the latest version of Golang and know the commands you're about to type, then read on.

Official binary distributions are available for the FreeBSD (release 10-STABLE and above), Linux, macOS (10.10 and above), and Windows operating systems and the 32-bit (386) and 64-bit (amd64) x86 processor architectures.

First, you need to download the archive from here.

Golang Website

As we're about to install Golang on Linux Mint, then choose go1.12.4.linux-amd64.tar.gz.

After the archive downloaded successfully, extract it to your preferred directory. For example, I'm using /opt directory.

$ sudo tar -C /opt -xzf go1.12.4.linux-amd64.tar.gz

Next, you'll need to add /opt/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/opt/go/bin

Note: changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as $ source $HOME/.profile.

That's it, Go should've successfully installed on your system.

Testing

To make sure we've installed Go correctly, we can set up a workspace and building a simple program, as follows.

Create your workspace directory, $HOME/go. (If you'd like to use a different directory, you will need to set the GOPATH environment variable.)

Next, make the directory src/hello inside your workspace, and in that directory create a file named hello.go that looks like:

package main

import "fmt"

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

Then build it with the go tool:

$ cd $HOME/go/src/hello
$ go build

The command above will build an executable named hello in the directory alongside your source code. Run it to see the greeting:

$ ./hello
hello, world

If you see the "hello, world" message then your Go installation is working. You can run go install to install the binary into your workspace's bin directory or $ go clean -i to remove it.

Final Words

I hope that you now know how to install Go (Golang) on Linux Mint 19. If you run into any issues or have any feedback feel free to drop a comment below.

Tags
Share:

0 comment

Leave a reply

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