Go

Hello, Go!

Published on August 1st, 2020


After reading a lot of articles about using Go as a programming language to write high-performing concurrent web applications and services yet maintain a clean codebase, I thought of starting to learn it to see what's it about and so far, it's been great!

This mini-tutorial will show you how to install Go on your system and write a simple "Hello, World" program.

Note: This tutorial will go through installing Go on Linux.

Install Go

Make sure you have the latest updates and upgrades on your Linux system before installing Go.

You need to download the binary file from their official package. Find the version that suits your OS and architecture.


cd /tmp
wget https://golang.org/dl/go1.14.6.linux-amd64.tar.gz

Extract the downloaded archive and install it in the /usr/local directory (as per the standards):


sudo tar -xvf go1.14.6.linux-amd64.tar.gz
sudo mv go /usr/local

Set up environment

Now, we need to set up three variables:

  • GOROOT: Location of where your Go package is installed.
  • GOPATH: Location of your work directory.'
  • PATH: Tells bash on where to look for programs that are being executed.

Open up your .bashrc file and add these lines at the end of the file:


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

Save your .bashrc file and update the current shell session:


source ~/.bashrc

Verify your installation

Type the following command to ensure that your Go installation is successful:


go version go1.14.6 linux/amd64

Hello, world!

As per traditions, whenever you learn a programming language, you start off with a "Hello, world!" program, so here it is:


package main

import "fmt"

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

Conclusion

I just started learning it today and at first, you might have the tendency to write code in Go the same way you write in any other language but beware, it doesn't work that way. It forces you to write good code including the way it formats the code using the gofmt tool.

Lots of cool things are coming!

Stay tuned for more.