How to Pass Data to Templates in Go

Web
Pass data to Go templates by creating a struct and executing the template with that struct as the data argument.

Pass data to Go templates by creating a struct with the data, then calling template.Execute with the struct as the second argument.

type Data struct {
    Name string
    Age  int
}

tmpl := template.Must(template.ParseFiles("template.html"))
data := Data{Name: "Alice", Age: 30}
err := tmpl.Execute(os.Stdout, data)