GoLang Templating Made Easy

Golang provides the package text/template for generating textual output from an object. It is used in many popular OpenSource projects like Kubernetes Ingress-NGIX and Hugo.

It allows us to avoid doing extra work when generating similar forms. We only change/process just what we need.

I will go over a few of the things I’ve learned while using it.

“person holding four photos” by Josh Hild on Unsplash

I have created some executable examples which can be seen here. Each example is built off of the previous. I will be going over the following topics:

  • Creating a Template from a Golang Object
  • Creating a Template Function
  • Creating a Sub-Template

The examples will consist of:

  • zoo.go: contains the logic for generating the template
  • zoo.tmpl: contains the actual template

Creating a Template from an Object

An object or range of objects can be used with a template to create a textual output.

Pretty much its as simple as a template recognizing the object being passed in and drawing attributes from the object to create wanted text. It is very useful for reports or configurations.

Let’s say we have the following structs:

and the following template:

we can loop through each of the Animals and generate a Report containing those animals. When executing the template, we will get the following output:

For a detailed example you can run, checkout example 1.

Creating a Template Function

Template functions are very useful for processing Objects passed within the template. With these functions we can run some logic before passing objects to the template.

Photo by Matthew Cabret on Unsplash

Suppose the zoo only wanted to allow animals which are suitable for the climate in the area of the zoo’s location. We can create a function to sort which animals belong in the zoo, so we can generate a template of the acceptable animals.

The template function would look as follows:

and it can be used in a template as follows:

We can see that before ranging through the animals to print, we run the getAcceptableAnimals function, passing the all the animals within a zoo, and the zoo’s climate. The we pass the result of that function(an array of animals) to the loop instead of zoo.Animals.

this will give us which animals are acceptable in the zoo:

For a detailed example you can run, checkout example 2.

Creating a Sub-Template

A sub-template would be a template that is loaded within another template.

This is useful because, we can move all common code to another template and reduce duplication of code if the sub-template is used several times in the main template.

In the following template, you can see how the the “ANIMALS” sub-template is defined, and used twice in the main template with the template syntax. This allows use to avoid duplicating the traversal of animals.

Also, note that Sub-Templates can be in other .tmpl files. You will just need to pass those files into the ParseFiles() function seen in the example.

For a detailed example you can run, checkout example 3. Note, I have added different template functions to process which animals should and should not be in the zoo.

For more details on using templating you can checkout some of the following resources:

Thank you for reading and I hope you enjoyed 😃

Senior Technical Marketing @ GitLab 🦊, Developer @ home, Keeping ATX Weird 😜

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store