Simple
t := template.Must(template.New("greet").Parse("hello {{.Name}}\n"))
t.Execute(os.Stdout, map[string]string{"Name": "Ada"})
text/templateGeneric text templating. Use html/template instead for anything rendered as HTML (auto-escapes).
t := template.Must(template.New("greet").Parse("hello {{.Name}}\n"))
t.Execute(os.Stdout, map[string]string{"Name": "Ada"})
t := template.Must(template.ParseFiles("layout.tmpl", "body.tmpl"))
t.ExecuteTemplate(os.Stdout, "layout.tmpl", data)
//go:embed tmpl/*.tmpl
var tmplFS embed.FS
t := template.Must(template.ParseFS(tmplFS, "tmpl/*.tmpl"))
{{if .LoggedIn}}hi {{.Name}}{{else}}sign in{{end}}
{{range .Items}}- {{.}}
{{else}}no items{{end}}
{{with .User}}{{.Email}}{{end}}
{{.Title | printf "%q"}}
{{len .Items}}
{{index . "key"}}
{{$name := .Name}}
{{range $i, $v := .Items}}{{$i}}={{$v}} {{end}}
funcs := template.FuncMap{
"upper": strings.ToUpper,
"join": strings.Join,
}
t := template.Must(template.New("x").Funcs(funcs).Parse("{{upper .Name}}"))
{{define "header"}}<h1>{{.Title}}</h1>{{end}}
{{template "header" .}}
{{block "content" .}}default content{{end}}