I'm trying to write a template (using html/template) and passing it a struct that has some methods attached to it, many of which return multiple values. Is there any way of accessing these from within the template? I'd like to be able to do something like:
package main
import (
"fmt"
"os"
"text/template"
)
type Foo struct {
Name string
}
func (f Foo) Baz() (int, int) {
return 1, 5
}
const tmpl = `Name: {{.Name}}, Ints: {{$a, $b := .Baz}}{{$a}}, {{b}}`
func main() {
f := Foo{"Foo"}
t, err := template.New("test").Parse(tmpl)
if err != nil {
fmt.Println(err)
}
t.Execute(os.Stdout, f)
}
But obviously this doesn't work. Is there no way around it?
I've considered creating an anonymous struct in my code:
data := struct {
Foo
a int
b int
}{
f,
0,
0,
}
data.a, data.b = f.Baz()
And passing that in, but would much prefer to have something in the template. Any ideas? I also tried writing a wrapper function which I added to funcMaps but could never get that to work at all.
Thanks for any suggestions!
wrapper function
? show your current works on it and let people help you work it out. – Rohde