Let’s Go(lang)

I decided I wanted to learn Go! It’s been far too long since I picked up another language so let’s start off at the main site: https://golang.org/

(Before going through the installation below, if you’re interested in a containerised dev environment instead, have a look at go-watcher and check their Docker examples there in which case you can skip the installation step of go and rather install Docker if you don’t already have it running.)

Install Go (using the getting started guide above). You can of course install on any operating system you’re using so just follow the steps that match your environment. From the Manjaro perspective, that was just finding and installing the go package using pacman.

From here let’s look at our IDE. You can even use a standard text editor if you want but with all the great free options now, you may as well install VSCode, Atom, vim or emacs unless you’re lucky enough to have a full version of Idea or the like, though the lighter weight IDE’s will be more than enough for now.

As part of your IDE setup, have a look for a go plugin as well. I’ve gone with vim-go installed using Vundle, nice and easy.

Finally we get to the hello world section but if you’ve dipped your fingers into even just a few, you’re likely sick of these by now. As always, go does not disappoint though and has a very good Tour of Go tutorial, which of course starts on page 1 with:

package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")

If nothing else, this will help confirm that you’ve setup your environment correctly. Save this, let’s call it main.go, and then on a command line, run ‘go build’ and you should have an executable (on linux, running it simply with ‘./main’).
Here’s a great screen cast from the Go getting started guide:

Writing, building, installing and testing Go code

From here go through the rest of the tour and enjoy what you learn. As with any language, especially opensource languages, you will be able to find tons more online so feel free to look around for whatever interests you as a starting point into Go.

Having been working on a few Angular projects recently I thought I’d look into using Go for the back-end Restful API. One of the first things you’re going to want to have when building an API is an easy way to test it. Not unit testing of course… (you should have already seen some of that in the video above otherwise check up more here)

I mean just simple methods to ensure you can call your endpoints and see that they are functioning. You can use curl or the like but I prefer to use Postman, especially because it’s cloud based cross-platform nature means I can save my tests across all my projects and run them from any machines or operating systems.

Back to our API: I found a number of great alternatives for how to go about this… A nice simple take, not worrying about data persistence just yet was this walk-through by Francis Sunday: Building a RESTful API with Golang
A more complete option and great next step looking at using a SQLite DB was this option by Etienne Rouzeaud: How to create a basic RESTful API in Go
Lastly, here’s a security focussed option with Auth0 and Angular from the Auth0 blog: Golang & Angular Series – Part 1: Developing and Securing Golang APIs and Golang & Angular Series – Part 2: Developing and Securing Angular Apps

At this point I’m getting really fed up with having to build and run every time I want to test a change so I had a look into some sort of watch mechanism and found go-watcher.

Other than the fact that this tool can now build and run your package as you work on it, another great thing they’ve done is have a docker example so that you’d even be able to use this and code your apps without actually installing Go on your machine at all!

I won’t go into too much detail, for our case the go-watcher container will do, but for more information this article by Mike Zazon walks you down a thought process of how to build your container and what decisions you can make to work with your particular goal: Containerize This! How to build Golang Dockerfiles

Now, the world is obviously your oyster from here, but for interest sake, I thought I’d merge this work with something else I’ve been playing with recently.

If you’ve recently played with a Raspberry Pi (and if you’ve ever had to write an image to a USB stick or sdcard) you’ll likely have at least heard about Etcher, the massively simplified GUI for doing this if you’re not too keen on playing with dd or the like.

I’m not sure of the history (and why I hadn’t heard of Balena until now, possibly whatever changes where involved moving from the previously named Resin.io) but the makers of Etcher (or at least the current owners) have released balenaCloud with some great IOT tools letting you essentially build an IOT fleet for multiple applications with a remarkably simple setup process and management interface.

I won’t go into detail now but in the case of a Raspberry Pi 3 and Go, have a look at the great Balena documentation here: Get started with Raspberry Pi 3 and Go. It’s as simple as setting up your app, downloading and installing your balenaOS image and then adding the Balena remote for your app to your git repository so that you can push your branch to your app which would then build your containers as you have scripted, on the devices you’ve assigned to that app.

Next steps would be to look into multi-container options and setting up a great load balanced IOT test bed for your apps going forward. I’m hoping to have some time to play with this and will write somethin up when I get to it: Getting started with multicontainer on balena

So that’s it for now. I hope you have a ton of fun looking into Go and let us know what you manage to do with your new found knowledge!

Leave a Reply

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

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