Custom functions

suggest change

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

main.go

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)
	}

Output:

5 + 5 = 10

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents