Welcome to our deep dive into Go Template Actions! In this lesson, we'll explore how to make your Go programs more dynamic and efficient using template actions. By the end of this tutorial, you'll be able to manipulate strings, execute code, and even call functions using templates.
Let's start from the beginning.
Go template actions are a powerful feature that allows you to embed Go code within double curly braces {{ }}. They are primarily used to format, generate, and manipulate strings, making them an essential tool for developers working with text-based data.
To get started, let's look at a simple example:
package main
import "html/template"
func main() {
t := template.Must(template.New("example").Parse(`
Hello, {{ .Name }}!
You are {{ .Age }} years old.
`))
data := struct {
Name string
Age int
} {
Name: "John",
Age: 30,
}
t.Execute(os.Stdout, data)
}In this example, we create a new template, replace {{ .Name }} and {{ .Age }} with the data from our data struct, and print the result to the standard output.
Go offers several types of template actions, each serving a unique purpose:
{{ .Name }}){{ . | json }}){{ range . }}){{ template "myfunc" . }})In the following sections, we'll dive into each type and provide practical examples to help solidify your understanding.
Simple actions are the most basic type of template action. They allow you to insert data directly into the template, as shown in our example above.
What is a simple action in Go Template Actions?
Pipe actions are used to process the data before it is inserted into the template. They consist of a pipe symbol | followed by the action name.
What is a pipe action in Go Template Actions?
Control structures in Go templates allow you to iterate through data and make conditional decisions.
What are control structures in Go Template Actions?
Custom functions in Go templates allow you to reuse code and perform complex operations that are not built-in.
What are custom functions in Go Template Actions?
Now that you understand the different types of template actions, let's look at a more complex, real-world example:
package main
import (
"html/template"
"os"
"strings"
)
type Person struct {
Name string
Age int
Hobbies []string
}
func main() {
people := []Person{
{Name: "John", Age: 30, Hobbies: []string{"Reading", "Coding"}},
{Name: "Jane", Age: 25, Hobbies: []string{"Hiking", "Painting"}},
{Name: "Bob", Age: 35, Hobbies: []string{"Gardening", "Fishing"}},
}
t := template.Must(template.New("example").Parse(`
<ul>
{{ range . }}
<li>{{ .Name }}, {{ .Age }} years old. Hobbies: {{ range .Hobbies }}{{ . }} {{ end }}
{{ end }}
</ul>
`))
t.Execute(os.Stdout, people)
}In this example, we use a range control structure to iterate through a slice of Person structs and display their names, ages, and hobbies in an ordered list.
Go Template Actions are a powerful feature that can make your Go programs more dynamic and efficient. By mastering simple actions, pipe actions, control structures, and custom functions, you'll be well-equipped to tackle real-world problems and make your code more maintainable.
Happy coding, and remember: practice makes perfect!