Using methods with multiple return values

You won't be able to call a function that returns two values in a template unless one of those values is an error. This is so that your template is guaranteed to work at runtime. There is a great answer that explains that here, if you're interested.

To solve your problem you need to either 1) break your function into two separate getter functions that you can call in the appropriate place in your template; or 2) have your function return a simple struct with the values inside.

I can't tell which would be better for you because I really have no idea what your implementation requires. Foo and Baz don't give many clues. ;)

Here is a quick-n-dirty example of option one:

type Foo struct {
    Name string
}

func (f Foo) GetA() (int) {
    return 1
}

func (f Foo) GetB() (int) {
    return 5
}

And then modify the template accordingly:

const tmpl = `Name: {{.Name}}, Ints: {{.GetA}}, {{.GetB}}`

Hopefully this is of some help. :)


There is also possibility to return struct with multiple fields and use them.

type Result struct {
    First string
    Second string
}

func GetResult() Result {
     return Result{First: "first", Second: "second"}
}

And then use in template

{{$result := GetResult}}
{{$result.First}} - {{$result.Second}}