Go Template Preview (or gotemplate.io) is a small tool for Go developers.
It aids them in writing text and html templates.
When we talk about "Go templates", there's three things we need to know:
Templates are pre-defined blobs of text with some logic embedded in them, which allows developers customize output by passing some variable data to the template renderer.
Here's an example of a Go template:
Now we need to define the data we will feed the template, we will do this in JSON:
And now, we render the template:
Feel free to edit the template or the data. They're both interactive!
Here are some examples of software that leverage Go templates:
You may be interested in using Go templates if:
Here's an example you can copy-paste:
Paste that into a main.go
file and run it with:
You should see the following output:
Like any other templating system, Go templates can quickly get very complex, especially when you start using logic like loops or conditionals in them.
Depending on how you use Go templates (e.g.: if you load templates into memory once during startup, or use embed), you may need to recompile your Go program for every single change you make, which can slow you down a lot.
Go Template Preview lets you write Go templates and pre-visualize them live.
You don't have to modify your existing code, install anything or recompile... Nothing!
You don't even have to be a Go developer to need Go Template Preview! If you're writing a Helm chart, or if you are configuring Prometheus' Alertmanager, you may be configuring custom notification templates for alerts.
Actually, the latter is what I was doing when I found I needed a tool like gotemplate.io.
And so I built it!
Huh? So you want to know how the sausage is made... here's how.