My Journey Learning The Go Programming Language

The key to learning anything new is finding a way to engage with the material. Some people learn by reading and some learn by doing. I typically fall into the latter half and I’ve typically been more successful in mentoring people who learn in a more hands-on way.

A recent Tweet I made led to a chat which somehow led to a famous Open Source developer getting me kickstarted into the world of developing in the Go Programming Language. This post tracks some of my journey for the last week (my first week of learning Go).

There is no real one-size-fits-all programming language out there. Every one has strengths and weaknesses. I personally love developing in C and that probably comes from my origins of working so close with hardware, I started my career writing 8-bit assembly language. It is a great fit for the high performance database work that I currently do in my day job. But it is not idea for rapid development and low barrier of entry. It is especially difficult to make it safe and secure in a web environment.

Having played with Go for a week now I would say that it is a great fit for rapid development of cloud services. This makes a lot of sense given its origins and I wish it was as well baked 8 years ago when I was leading PaaS team for HP Cloud.

Go is definitely designed in a way to enforce you into a single way of doing things. That is fine, it has been very difficult for me to let go as I have a specific way I like laying out and developing projects. But at the same time enforcing things from the beginning helps with code navigation for people new to certain code bases.

My Project

Since I learn hands-on I needed a project to learn as I develop. I first patched a project my mentor had, but I really needed something I could develop from-scratch to really delve into the language.

Last year I developed a lightweight simple library in C which talks to Amazon (and compatible) S3 servers using REST. This was very rapidly developed, the code isn’t very clean, but it is pretty functional and is used in a few MariaDB projects. I knew the REST API pretty well so I figured writing a Go module for this would be a great starting point. I have therefore developed a New-BSD licensed module called GoS3.

This new module is pretty basic right now, at the time of writing it had a couple of limitations, such as the List function only getting the first 1000 objects. But I’ve only had several hours this week to develop and learn. Making something perfect was not the goal.

The Good

Writing this library has been extremely easy. The standard library has been well thought-out and I very quickly got to the point where I was knocking out new functionality in minutes. Once you get the hang of how things like the relationship between package names and directories works, as well as public / private and struct methods it becomes quite natural.

One really go thing about the code layout is examples. If you have a function called “Get” you can create a function called “ExampleGet” and not only is this executed when running go test but it is automatically added as examples in documentation.

This leads neatly to documentation, go enforces code comments in a certain format. go vet will tell you if are doing something that isn’t quite right in a nice, verbose way.

go fmt is another useful tool which enforces Go’s code format, this does also mean tabs, not spaces. Whilst I am a fan of spaces over tabs I don’t mind that much as long as a code base is consistent.

The most powerful thing about Go is “Goroutines”. I’m very used to developing applications that use event loops and cooperative yielding to do multi-tasking in a single thread. This is a very powerful tool, but it can be very difficult to do. In Go this becomes a breeze, it is built into the core language in something called Goroutines. This is one of the main things that attracted me to the language. Unfortunately I haven’t been able to give it a big try yet, but I hope to very soon.

The Bad

Sometimes enforcing certain things like directory layouts on you can be painful, and I stumbled a lot on this. I came across a lot of tutorials that appear to be written for previous iterations of Go or by people who were experts and skimmed over the stuff that I wanted to know about. I’m still not quite sure I’m doing everything right, but “it works”.

I think a lot of the help out there has been written by people who have come from a Java style world into a Go style world. It would have helped me more to have a C -> Go introduction. But a few sites combined with help from my mentor really helped me out. The Go Tour is very good. But a lot of the help there is geared toward you developing applications, not modules.

The Unknown

In C it was very clear to me what will perform well and what will not. This is not clear to me in Go yet and I haven’t yet done any research into performance metrics for Go.

I have been told by my mentor that the garbage collector is very good, but it is still a garbage collector. I’ve hit many pitfalls with them in the past in other languages, I have a slight concern over what I’ll hit in the future.

go.mod files… I still don’t have the hang of these yet.

Conclusion

It has only been a week of an hour or two here and there but I’ve really enjoyed learning Go. I plan to take this journey further and learn more. I just hope I can find a good application which needs developing and fits nicely into Go’s strengths.

The gopher logo was created by Renée French is used under a CC Attribution 3.0 license.

Published by

LinuxJedi

Lead Software Engineer / Manager for the MariaDB Corporation and an Open Source Software advocate.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.