Text Templates

This Go code demonstrates the use of the text/template package to define and execute templates. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"os"
	"text/template"
)

// The main function, where the execution of the program begins.
func main() {
	// Creating a new template named "t1" and parsing a simple template string.
	t1 := template.New("t1")
	t1, err := t1.Parse("Value is {{.}}\n")
	if err != nil {
		panic(err)
	}

	// Using template.Must to simplify error handling and parsing another template string.
	t1 = template.Must(t1.Parse("Value: {{.}}\n"))

	// Executing the template with different values and printing the result to os.Stdout.
	t1.Execute(os.Stdout, "some text")
	t1.Execute(os.Stdout, 5)
	t1.Execute(os.Stdout, []string{
		"Go",
		"Rust",
		"C++",
		"C#",
	})

	// Creating a function to simplify template creation.
	Create := func(name, t string) *template.Template {
		return template.Must(template.New(name).Parse(t))
	}

	// Creating a new template "t2" using the Create function and executing it with a struct and a map.
	t2 := Create("t2", "Name: {{.Name}}\n")
	t2.Execute(os.Stdout, struct {
		Name string
	}{"Jane Doe"})

	t2.Execute(os.Stdout, map[string]string{
		"Name": "Mickey Mouse",
	})

	// Creating a new template "t3" using the Create function with a conditional statement and executing it.
	t3 := Create("t3", "{{if . -}} yes {{else -}} no {{end}}\n")
	t3.Execute(os.Stdout, "not empty")
	t3.Execute(os.Stdout, "")

	// Creating a new template "t4" using the Create function with a range statement and executing it.
	t4 := Create("t4", "Range: {{range .}}{{.}} {{end}}\n")
	t4.Execute(os.Stdout,
		[]string{
			"Go",
			"Rust",
			"C++",
			"C#",
		})
}

Output

Value: some text
Value: 5
Value: [Go Rust C++ C#]
Name: Jane Doe
Name: Mickey Mouse
yes 
no 
Range: Go Rust C++ C# 

Explanation:

  1. Creating and Parsing Templates:

    • t1 is created with template.New("t1") and parsed with t1.Parse("Value is {{.}}\n").

    • template.Must is used to simplify error handling and parse another template string.

  2. Executing Templates:

    • The Execute function is used to execute templates with different values, printing the results to os.Stdout.

  3. Template Creation Function:

    • The Create function is defined to simplify template creation using template.Must.

  4. Using Struct and Map:

    • t2 is created using the Create function, and it is executed with both a struct and a map as input.

  5. Conditional Statement in Template:

    • t3 is created using the Create function with a conditional statement ({{if . -}} yes {{else -}} no {{end}}).

  6. Range Statement in Template:

    • t4 is created using the Create function with a range statement ({{range .}}{{.}} {{end}}), and it is executed with a slice of strings.

These examples showcase the flexibility of the text/template package for defining and executing templates with various data structures and control structures.

Last updated