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.
A FuncMap is a dictionary that links custom Go functions to names used inside your HTML or text templates. It matters because it lets you extend the template engine with your own logic, like formatting dates or calculating totals, without writing complex code inside the template file itself. Think of it as adding new tools to a toolbox so your templates can use them.