This repository demonstrates how to use loops in Go's text/template
package. It shows how to iterate over a collection of data and render each item in a template, providing dynamic content generation based on input data.
- This example covers basic loop constructs in Go templates using the `range` keyword.
- It includes a demonstration of looping over a slice of structs to display tasks and their statuses.
- Showcases how to render dynamic data in a structured format within templates.
package main
import (
"fmt"
"os"
"text/template"
)
type Task struct {
Title string
Status string
}
type Person1 struct {
Name string
Tasks []Task
}
func main() {
// This template uses range to loop over a slice of Task structs and list out each task with its status.
person := Person1{
Name: "Bob",
Tasks: []Task{
{"Task 1", "Completed"},
{"Task 2", "Pending"},
{"Task 3", "In Progress"},
},
}
tpl := `{{.Name}}'s Tasks:
{{range .Tasks}}- {{.Title}}: {{.Status}}
{{end}}`
t := template.Must(template.New("person").Parse(tpl))
err := t.Execute(os.Stdout, person)
if err != nil {
fmt.Println("Error executing template:", err)
}
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the
029_text_samples/002_loops_in_templates
directory:
cd go_sample_examples/029_text_samples/002_loops_in_templates
- Run the Go program:
go run 002_loops_in_templates.go
When you run the program, you should see the following output:
Bob's Tasks:
- Task 1: Completed
- Task 2: Pending
- Task 3: In Progress