Standard library / text/template

text/template.Template.Funcs()

Registers named functions before parsing. A function can return one value or a value and error. Expected stdout: 'ADA\n'.

Formal Syntax & Structure

go syntaxgo
func (t *Template) Funcs(funcMap FuncMap) *Template

Parameters & Attributes

NameTypeDescription
funcMapFuncMapNamed functions available to template actions.

Return Value

*Template

Registers named functions before parsing.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "strings"  "text/template")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  t := template.Must(template.New("greet").Funcs(template.FuncMap{"upper": strings.ToUpper}).Parse("{{upper .}}"))  check(t.Execute(os.Stdout, "Ada"))  fmt.Println()}
Go Reference