Golang has always been something on my list of things to learn and make use of within a production environment for some time. Recently I found myself in the perfect position to start learning it whilst also using it to solve a problem within the project I was working on.
This article will mainly be a brief overview about my views and thoughts of Golang and whether or not I would use it going forward to solve future problems.
There’s a project I have been working on which uses Angular to build the frontend Single Page App for an online store, with the backend powered by a number of APIs. One of the main problems was allowing end users’ the ability to authenticate to the OAuth API without exposing the client token and secret.
The solution was to create a Gateway between the SPA and the OAuth API – one of the proposed solutions would have been to create the gateway directly into NGINX configs with LUA using an NGINX module, this would inject the tokens and secrets into the request body and proxy to the API.
However this introduced a number of inflexible problems and increased the amount of work for NGINX, especially as we would have had to customize NGINX. So the second proposed solution was to create a gateway using Node.js which would act as a proxy, besides this is a language I already know, with a large community and all the tools I needed to quickly build the app.
But, some days after the initial build I updated my
node_modules to discover a dependency used by a packages’ dependency was returning a 404 – it turned out NPM had removed the package due to malicious code. This caused me to panic that a production server could have malicious code at this point I decided to Go with option three!
Option three, was to rewrite the whole gateway again – this time in Golang which would have required me to learn the language, and within just a couple of hours I had completed my task.
The Golang documentation is fantastic, using an IDE such as Goland really improved the learning experience although you could easily achieve the same with VSCode and some extensions. Writing unit tests was a piece of cake, Go has been designed with testing in mind but it involved understanding interfaces, which differs from an Object Oriented language – importing packages and namespacing things was really easy, understanding the
$GOPATH was pretty simple to change so I could build my projects how I usually do.
Makefiles aren’t new to me so literally the first thing I did was create one to speed up the compiling of my Go apps. In terms of the language itself, I’m used to TypeScript so structs were very simple for me to understand. I discovered that private visibility is implied with lowercase first character, and capital was public. But the best thing that amazed me was the ability to do Inversion of Control very simply.
Anyone who is interested in learning Go, honestly try it! I’m certainly going to be utilising it more often.