Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang templates "minus" function

I know that in go templates I can call function named add for expression like 1 + 1. But how named function for expression like 2 - 1?

like image 577
cnaize Avatar asked Jul 19 '14 07:07

cnaize


People also ask

What is Gomplate?

gomplate is a template renderer which supports a growing list of datasources, such as: JSON (including EJSON - encrypted JSON), YAML, AWS EC2 metadata, and secrets. Come chat with developers and community in the on and on !

What are the characteristic S of the GO text template package?

Overview. Package template implements data-driven templates for generating textual output. To generate HTML output, see package html/template, which has the same interface as this package but automatically secures HTML output against certain attacks. Templates are executed by applying them to a data structure.

What is go templating?

Go templates are a powerful method to customize output however you want, whether you're creating a web page, sending an e-mail, working with Buffalo, Go-Hugo, or just using some CLI such as kubectl. There're two packages operating with templates — text/template and html/template .


1 Answers

There is no add function included by default. You can however, easily write such functions yourself. For example:

tmpl := template.Must(template.New("").Funcs(template.FuncMap{
    "minus": func(a, b int) int {
        return a - b
    },
}).Parse("{{ minus 5 2 }}"))
tmpl.Execute(os.Stdout, nil)
like image 148
tux21b Avatar answered Sep 20 '22 07:09

tux21b