Quick actions

cmd+k|ctrl+k

Navigation

Languages

Text and HTML templates / Custom functions / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:16:47Z

Updated

2019-03-26T10:16:47Z

package main

import (
	"log"
	"os"
	"text/template"
)

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

func sum(x, y int) int {
	return x + y
}

func main() {
	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)
	}
}
INFO