Essential Go Text and HTML templates  Edit on GitHub      File Issue

Methods as data

In a template {{ .Foo }} will either access struct field Foo or call a function Foo():

var tmplStr = `Data from a field: '{{ .Field }}'
Data from a method: '{{ .Method }}'
`

t := template.New("method")
t, err := t.Parse(tmplStr)
if err != nil {
	log.Fatalf("template.Parse() failed with '%s'\n", err)
}

data := Data{
	Field: 5,
}

err = t.Execute(os.Stdout, data)
if err != nil {
	log.Fatalf("t.Execute() failed with '%s'\n", err)
}
Data from a field: '5'
Data from a method: 'data from a method'
  ↑ ↓ to navigate     ↵ to select     Esc to close