例如:
func main() {
t := template.Must(template.New("").Funcs(template.FuncMap{
"Deref": func(i *int) int { return *i },}).Parse(srC))
i := 5
m := map[String]interface{}{"ID": &i}
if err := t.Execute(os.Stdout,m); err != nil {
fmt.Println(err)
}
}
const src = `{{if eq 5 (Deref .ID)}}It's five.{{elsE}}Not five: {{.ID}}{{enD}}`
It's five.
或者你可以使用一个不同的自定义函数来获取指针和非指针,并进行比较,例如:
"Cmp": func(i *int,j int) bool { return *i == j },
并从模板中调用它:
{{if Cmp .ID 5}}It's five.{{elsE}}Not five: {{.ID}}{{enD}}
输出是一样的.在Go Playground上试试这些.