草庐IT

function - Golang 模板 "minus"函数

我知道在go模板中,我可以调用名为add的函数来表达像1+1这样的表达式。但是如何为2-1之类的表达式命名函数? 最佳答案 默认没有add函数。但是,您可以自己轻松编写此类函数。例如:tmpl:=template.Must(template.New("").Funcs(template.FuncMap{"minus":func(a,bint)int{returna-b},}).Parse("{{minus52}}"))tmpl.Execute(os.Stdout,nil) 关于funct