Go has a built-in templating language, text/template, that implements templates with data and generates a text-based output. We use structs in order to define the data that we want to use within our templates. As with all things, Go input text is defined as UTF-8 and can be passed in as any format. We use double braces {{}} to denote actions that we want to perform on our data. A cursor, represented by ., allows us to add data to our template. These things combined create a powerful templating language that will allow us to reuse templates for many bits of code.
First, we are going to initialize our package, import our necessary dependencies, and define our struct for the data that we would like to pass into our template:
package main
import (
"fmt"
"os"
"text/template"
)
func main() {
type ToField struct {
Date string
Name...