Text Templates
package main
import (
"fmt"
"os"
"text/template"
)
type Person struct {
Name string
Age int
Country string
}
func main() {
person := Person{
Name: "John",
Age: 30,
Country: "USA",
}
tpl := "My name is {{.Name}} and I am {{.Age}} years old. I live in {{.Country}}.\n"
t := template.Must(template.New("person").Parse(tpl))
err := t.Execute(os.Stdout, person)
if err != nil {
fmt.Println("Error executing template:", err)
}
}Last updated