Skip to content

Text templates

The text/template package generates textual output from data using a template syntax. It is safe for HTML generation as well (use html/template for web).

Execute a template with struct data.

package main
import (
"os"
"text/template"
)
func main() {
type Person struct {
Name string
Age int
}
p := Person{"Alice", 30}
tmpl := template.Must(template.New("greeting").Parse("Hello {{.Name}}, you are {{.Age}} years old."))
tmpl.Execute(os.Stdout, p)
}
Terminal window
Hello Alice, you are 30 years old.