Go templates are a powerful tool to customize output the way you want it. It’s a builtin package implements data-driven templates. Templates are executed by applying them to a data structure. While there are articles covering the basics, I had a hard time findings material on more advanced use-cases, such as looping over complex structs or using a function in the template. This post aims to distill these advanced use-cases with examples.
The topic is large, and there is plenty of information online. This blog tries to keep it short and concise, and useful for experienced programmers that are new to Go. Coming to Go from Python introduced me to a new concept I didn’t have to put thought into. Python is a pass-by-object-reference language. You have no direct control over that. What this means is, when you pass an object (everything in Python is an object) to a function, you pass a reference to the object itself.