Go Template Actions 🎯

beginner
20 min

Go Template Actions 🎯

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.

Understanding Go Template Actions 📝

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.

Basic Template Action Example 💡

To get started, let's look at a simple example:

go
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.

Template Action Types 💡

Go offers several types of template actions, each serving a unique purpose:

  1. Simple actions (e.g., {{ .Name }})
  2. Pipe actions (e.g., {{ . | json }})
  3. Control structures (e.g., {{ range . }})
  4. Custom functions (e.g., {{ template "myfunc" . }})

In the following sections, we'll dive into each type and provide practical examples to help solidify your understanding.

Simple Actions 📝

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.

Quiz

Quick Quiz
Question 1 of 1

What is a simple action in Go Template Actions?

Pipe 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.

Quiz

Quick Quiz
Question 1 of 1

What is a pipe action in Go Template Actions?

Control Structures 📝

Control structures in Go templates allow you to iterate through data and make conditional decisions.

Quiz

Quick Quiz
Question 1 of 1

What are control structures in Go Template Actions?

Custom Functions 📝

Custom functions in Go templates allow you to reuse code and perform complex operations that are not built-in.

Quiz

Quick Quiz
Question 1 of 1

What are custom functions in Go Template Actions?

Putting It All Together: Real-World Example 💡

Now that you understand the different types of template actions, let's look at a more complex, real-world example:

go
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.

Conclusion ✅

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!