package main import ( "html/template" "os" ) func main() { tmpl, err := template.ParseFiles("index.html") if err != nil { panic(err) } data := struct { Title string Body string }{ Title: "My Title", Body: "Hello, World!", } err = tmpl.Execute(os.Stdout, data) if err != nil { panic(err) } }
package main import ( "html/template" "os" ) func main() { pages := []struct { Title string Body string }{ { Title: "Page 1", Body: "Content for page 1", }, { Title: "Page 2", Body: "Content for page 2", }, { Title: "Page 3", Body: "Content for page 3", }, } tmpl, err := template.ParseFiles("index.html") if err != nil { panic(err) } for _, page := range pages { err := tmpl.Execute(os.Stdout, page) if err != nil { panic(err) } } }In this example, we create a slice of structs that represent different pages on our website. We then loop over these pages and execute the same template with different data each time. Overall, the "html/template" package is a powerful tool for generating dynamic HTML content in Go. Its built-in support for template inheritance and data manipulation makes it a popular choice for web developers working with Go.