Essential Go Text and HTML templates  Suggest an edit

Custom functions

You can define custom functions and make them available in templates:

const tmplStr = `5 + 5 = {{ sum 5 .Arg }}
`

customFunctions := template.FuncMap{
	"sum": sum,
}

t := template.Must(template.New("func").Funcs(customFunctions).Parse(tmplStr))

data := struct {
	Arg int
}{
	Arg: 5,
}
err := t.Execute(os.Stdout, data)
if err != nil {
	log.Fatalf("t.Execute() failed with '%s'\n", err)
}
5 + 5 = 10
  ↑ ↓ to navigate     ↵ to select     Esc to close