First dive into Go

Heikki Hellgren
Mar 8 · 5 min read

One day last week I noticed that my TODO list was getting too big to keep it only in my memory so I started looking for app to put my notes to. One requirement for the app was that I could easily access it from command line on any machine and that the notes would be available on all of the machines. I found some “goodish” apps like Todo.txt and Taskwarrior but they just didn’t click for me. So I decided, hey, why not write one myself and learn something new while doing so.

I had heard of Go programming language from one of my dear friend (already long time ago) but never really had time to dig into it. “Now is as good time as any” I thought and headed to the internet wonderland in hope of learning something new.

Installation

Installation of Go is pretty simple and you can find instructions from https://golang.org/doc/install for all most used operating systems. I work mainly on my Ubuntu machine (at work and at home) and what Go installation does is that it creates “go” directory inside your home directory. This is the directory you will find sources and the executables of different go applications or packages you install. To use them from anywhere you should add the ~/go/bin to your $PATH environment variable.

Go provides tools to easily get packages from for example github with single command “go get <url>”. This will fetch the go package as well as its dependencies. In case it’s an application, the command will also by default build the binary for you to the go/bin folder. Cool, right?

API specification

Go has quite a lot of stuff built-in in the standard library and you can find all documentation from the Go website. Interesting about the specification and how it works is also the website https://godoc.org/ which hosts API specification also for packages located outside of the standard library. This also means that if you’re writing some open source Go, hosting of your API specification is already covered.

Dependency handling

If you are about to use other packages in your code, you need to have some sort of dependency handling. When I first started to introduce dependencies I bumped into golang dep tool and started with that. Later on I figured out that there is also built-in dependency handling support called Go modules which seems to be the right way to do it as your source code can live also outside the $GOPATH. Go modules requires version 1.11 or later to work so you should check your go version, update if necessary or use another dependency manager for go if you want to stick with older version.

The actual programming

Coming from mainly writing C++ code, the actual programming with Go took some time to adjust. The syntax differs quite a bit from C++ for example:

In Go it would translate something like this (don’t mind it not being perfect C++ or Go ;)):

As you may notice that in Go:

  • Main function must be located in package called main. Also it does not take any arguments

It might get some time to get familiar with but after a while it will start flowing like with any other language. Especially the error handling can be quite cumbersome with Go compared to languages with exceptions supported.

What I really love about Go is the compiler. It tells you a lot of different errors about your code for example unused variables or imports. It basically forces to write more optimized and clean code. It’s also super fast which makes the development easier.

But wait, it’s not all rainbows and unicorns with Go either. There are some functionality that I really miss (or didn’t find a standard library support) such as ordering of maps by key, converting string to uint type without need for unnecessary casting from strconv etc. There was also very strange thing going on when iterating maps: the iteration order was totally random (you can actually read more about it here)! These kind of things take a lot of time to figure out when working with a new language.

While it’s quite easy to get into coding with Go, there are some things I did not have need or time to study such as goroutines, channels, http functionality, database connections, image manipulation etc. These things would require some project to actually get familiar with them.

Final words

I really enjoyed my first touch with Go. I actually did manage to produce working (thought not complete) version of my TODO app. If you are interested, please check it out here.

Next time you’re starting a new software project you should consider Go as your choice of language. It doesn’t fit all purposes but is an excellent choice for many. And if nothing else, try it out and see how you feel!


If you liked the story, please press the ❤ button below (did you know that you can give more than one clap). Also please feel free to share this story!

About me

I am Heikki Hellgren, Software Expert and technology enthusiast working at Elektrobit Automotive. My interests are in software construction, tools, automatic testing and all the new and cool stuff like AI and autonomous driving. You can follow me on Medium and Twitter and check out my website for more information.

Heikki Hellgren

Written by

Father of two, husband and Expert @ Elektrobit Automotive. https://drodil.kapsi.fi