Why Go has become my favourite language for almost anything
A couple of years ago at work I was tasked to build the software of an IoT thing. The program had to run on a development board with an ARM Linux distribution and I had complete control on how to structure the project. At that time I was mainly a web developer and my strongest skill in terms of programming languages was Node.js, so my first instinct was to write that firmware in the language I knew the best.
But I started thinking about the downsides and the first problem that came to my mind was that we didn’t want to provide the source code of our project to our clients unless they submitted an explicit request. So interpreted languages like Python, Ruby and Node.js were out of the question. Another problem was that the program had to perform some high speed operations like GPIO interactions, so it had to be fast.
The first language that came to my mind was C , but at the time I was already pretty rusty and knowing that the program had to do a lot of different stuff (interactions with the electronic, webserver, WebSocket client, etc.), I wasn’t confident enough to do it using C.
Entering Go & love at first sight
So I decided to give Go a try and I took some online courses. The official website has a nice tour that gives you a good place to start learning it. Another useful resource might be Codecademy (I personally didn’t try that but I’ve used this platform in the past and it’s great!). I was no longer used to using pointers and strongly typed functional languages, but after a few hours I found out that Go is really simple to use, specifically for the following reasons:
It has the most complete standard library
The standard library provided with Go is so complete that it took me weeks before deciding to use an external module, and even then it was only for having better logs and managing WebSocket connections.
Go provides modules for parsing JSON and XML, making HTTP servers, managing command line arguments, encryption, RegExp, …, all ready to use straight out of the box. They are stable, fast and intuitive and the docs are well written.
Best time management
One of the worst nightmares for programmers is time management: adding days to a date, getting the difference in hours between to dates, formatting to string, parsing from string.
Go engineers really committed themselves to solving all these problems, and they pulled it off.
Go has basically two types of variables that help you managing dates: Time and Duration, the first defines a specific point in time while the last represents the time span between two instants.
It’s full of useful methods that allow you to add a Duration to a Time, subtracting a Time to another and getting the Duration, getting the hours of a Duration or formatting a Time to string using a specific format.
Many languages rely on widely used and tested external libraries (like moment for JavaScript) because the native time management is not intuitive or complete or thorough, I don’t really think Go needs anything else to what it already provides.
Cross compile
This is a feature that may sound useless to many people because you often write your applications for only one architecture since you know that it will run only on - let’s say - amd64 Linux servers. In my case my first project using Go had to be running on ARMv7 architecture running Linux, and cross compile really helped me speeding up the development by testing my program on my amd64 laptop. When I decided to run it on the development board I just had to set a couple of environment variables used by the Go compiler and I was ready to test my code on another architecture.
Data race detector
Being Go a programming language that highly leverages on concurrency with its goroutines, you may run into the problem of having two functions accessing the same variable at the same time. These are the hardest bugs to find in your code because depending on the case the outcome might be different.
According to the official documentation:
A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write.
If you build your Go code using the -race flag, the output binary will contain a data race detector, i.e. an automatism for which you’ll be notified if this problem happens. The notification will contain the full stack trace of the functions that are concurrently accessing the same variable, making your bugs catching so much easier!
Conclusion
In the last few years I’ve been using Go I started building softwares for development boards, then REST API servers, then WebSocket applications and scripts and command line utilities. Go has probably become my main programming language: it’s fun to use, easy to debug, fast and versatile.
I don’t think it’s perfect, other languages perform some tasks better, and I still write some stuff using Node.js or Python, but I really like the direction that Go has been taking over the years and I hope its community will grow bigger and bigger!