Real-World Go with Referenced Functions

coding
golang
How I figured out structuring real-world Go projects with functions split across multiple files.
Author

Alex Strick van Linschoten

Published

June 23, 2018

Writing small test scenarios and packages in the Go Playground, or in a single main.go file is all good and well, but in the real world things are more complex. (Or so I heard).

Today I wanted to get a brief sense of how that all works. I had some sense of how to make this all work when working through the GreaterCommons course, but it proved harder when the training wheels were taken away.

This is what I emerged with at the end of my experiments. It is a main.go file that references extended.go. This ended up being dependent on knowing a bunch of setup quirks and techniques that aren’t particularly well signposted for beginners. I ended up finding a tremendously helpful document on the main Go site entitled “How to Write Go Code”. They don’t signpost it much, especially when compared to ‘Effective Go’ or the general specification.

I worked my way through the official examples. Luckily they were simple enough that I could follow all of the logic. Everything was incrementally constructed, slowly becoming more complex. I learnt the following:

I am already demonstrating some sloppy habits with my documentation, tests, benchmarks and examples, I realise. For these kinds of toy demonstration examples it is less mission-critical, but there feels like a vague principle at stake.

My next Go exploration project will probably be the final version of an Exercism exercise I’ve been working on for a while.