How to Use Template Functions (FuncMap) in Go

Web
Register custom Go functions in a template.FuncMap and pass them to template.New().Funcs() to use them inside your templates.

Use template.FuncMap to register custom functions with a *template.Template before executing it.

func main() {
	funcs := template.FuncMap{
		"upper": strings.ToUpper,
	}
	tmpl := template.Must(template.New("t").Funcs(funcs).Parse("Hello {{upper "world"}}"))
	tmpl.Execute(os.Stdout, nil)
}

The Funcs method returns the template, allowing method chaining to register functions before parsing or executing.