One way is to register a custom function that separates the pointer so that you can compare the result with what you want or something else with it.
For instance:
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 five.{{else}}Not five: {{.ID}}{{end}}`
Output:
It five.
Alternatively, you can use another custom function that will take a pointer and not a pointer, and perform a comparison, for example:
"Cmp": func(i *int, j int) bool { return *i == j },
And calling it from the template:
{{if Cmp .ID 5}}It five.{{else}}Not five: {{.ID}}{{end}}
The output is the same. Try them on the go playground .
source share