Install Go on Ubuntu

Install Go and set up paths on Ubuntu.

Go, also known as Golang, is a statically typed, compiled programming language.

Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Download latest package for Go

1
curl -O https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz

Extract archive to /usr/local

1
tar -C /usr/local -xzf go1.12.9.linux-amd64.tar.gz

Path Setup

Add /usr/local/go/bin to the PATH environment variable so that you can execute go command at any path. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile

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

Then we need to setup GOPATH. This is where all the go modules are downloaded. Add the following command to system path.

1
2
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Run Hello World

create and go to src/hello directory. Then create hello.go file

hello.go

1
2
3
4
5
6
7
package main

import "fmt"

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

use go build command to build the binary

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

Now execute the binary file. hello, world should be printed on the console.

1
2
$ ./hello
hello, world

Reference