Getting started with Go (Ubuntu 18.04)

Go is an open source programming language that was made by Google in 2009 to make life easier on some of the projects they were working on. According to some of the Google engineers, there hadn’t yet been a mainstream programming language that was able to offer efficient compilation and execution, at the same time as being less complex to use. This led to the creation of one of the more recently popular languages in Go. A quick google search and you can find Go on a good amount of “Best Languages to learn this year” blog posts and articles. Its being quickly adopted in web development and its being used to create DevOps & monitoring tools like Docker, Kubernetes, and Prometheus. It’s also being promoted by some of the top companies like Twitter, Uber, and Dropbox which make it a strong option for developers looking to pick up a new language. Let’s take a look at everything you need to install Go and get started.

Download Go

First, what we wanna do is download the Go programming language. We’ll do that by using the tool wget to get the most current version (1.13.7 at the time of writing) after making sure we’re in the right folder.

$ cd ~/Downloads
$ wget -c https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz

Verify Checksum

Next, we need to make sure we have the right file using the checksum command. This ensures that nothing wasn’t tampered with while downloading it.

$ shasum -a 256 go1.13.7.linux-amd64.tar.gz 
b3dd4bd781a0271b33168e627f7f43886b4c5d1c794a4015abf34e99c6526ca3 go1.13.7.linux-amd64.tar.gz

Install Go tools

Now that we’ve made sure we got the archive file downloaded safe and sound, let’s unpack it to install Go tools so we can run some commands in the terminal. We’ll do this by extracting the archive into /usr/local so the location will be /usr/local/go.

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

Inside the go /usr/local/go there is a bin folder, we want to add that to the PATH environment variable for our current user account by editing the .profile file in our home directory. I like to use nano for something like this.

$ nano $HOME/.profile

That will open up a text file or create one if it doesn’t already exist. Add this line to the bottom of the file.

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

Press Ctrl+X to exit the file, and Y to confirm.  We’ll also want to run the source command to avoid having to log out and log back in for the changes to take effect.

$ source $HOME/.profile

Make sure everything works

The final step is to test and make sure everything works! We’re going to create a workspace in our home directory and eventually create a Go file that will print something. Let’s start by creating that workspace and an additional directory inside of it

$ mkdir $HOME/go
$ cd $HOME/go && cd mkdir src/hello

All that’s left now is to cd into src/hello and create our Go file. 

$ cd src/hello

However you’d like, create a file in this directory named hello.go and type this in.

package main

import “fmt”

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

Congratulations, you’ve written your first Go file, now let’s get it to run. Run the build command through Go, we’ll end up with a binary file that we can run. Then we’ll run it and see our message.

$ go build hello.go
$ ./hello
Hello, world!

Nice! You did it. Hopefully you enjoyed this tutorial. If there’s anything you think I missed or have any feedback, feel free to comment below. My next post will talk about go modules and importing packages. Thanks for reading!

Leave a Comment

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