package main import ( "html/template" "os" ) func main() { tmpl, _ := template.New("test").Parse("{{.}}") tmpl.Execute(os.Stdout, "Hello, world!") }
package main import ( "html/template" "os" ) type Person struct { Name string Age int Birthday string } func main() { tmpl, _ := template.New("test").Parse("This example creates a template with a more complex structure, including HTML elements and Go code to access fields of a struct. It then creates a Person object with some test data and calls the "Execute" method with os.Stdout and the Person object as arguments. The resulting output will be an HTML document with the name, age, and birthday of the Person object displayed in appropriate HTML elements. In summary, the "html/template" package in Go provides a powerful tool for generating dynamic HTML content from Go code. The "Execute" method of the template type is essential for applying a template to a data object and generating output.{{.Name}}
{{.Age}} years old, born on {{.Birthday}}
") data := Person{"Alan Turing", 41, "June 23, 1912"} tmpl.Execute(os.Stdout, data) }