Dereferencing pointers in a golang text/template
Asked Answered
N

1

17

Inside a golang template when simply outputting values it seems that pointers are automatically dereferenced. When .ID is a pointer to an int,

{{.ID}} outputs 5

But when I try to use it in a pipeline, {{if eq .ID 5}} I get an error.

executing "mytemplate" at <eq .ID 5>: error calling eq: invalid type for comparison

How do I do a dereference of a pointer inside a template pipeline?

Nan answered 24/2, 2016 at 18:15 Comment(0)
K
16

One way is to register a custom function which dereferences the pointer, so you can compare the result to whatever you want to or do anything else with it.

For example:

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}}`

Output:

It's five.

Alternatively you could use a different custom function which would take a pointer and a non-pointer, and do the comparision, e.g.:

    "Cmp":   func(i *int, j int) bool { return *i == j },

And calling it from the template:

{{if Cmp .ID 5}}It's five.{{else}}Not five: {{.ID}}{{end}}

Output is the same. Try these on the Go Playground.

Kelvinkelwen answered 24/2, 2016 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.