Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereferencing pointers in a golang text/template

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?

like image 788
Aruna Herath Avatar asked Feb 24 '16 18:02

Aruna Herath


1 Answers

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.

like image 85
icza Avatar answered Oct 14 '22 07:10

icza