You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go version: go version go1.4.2 darwin/amd64
OS: OS X Yosemite Version 10.10.1
When using {{call ...}} in a template with a struct method the template parser fails to send any arguments to the method. This does work correctly if the method returns a function, or if the function you want to call is set to an attribute.
If this isn't intended to work, the error message could be a little clearer.
I admit the error message is poor but it is working as intended. You can't call something that doesn't have type func. If you look at case C, you'll see that it's missing one layer of "func" relative to cases A and B. Or, to put it another way, the evaluation of call's first argument must yield something of type 'func'. What it finds is a method, so it calls the method (without the tail of the action, which will become arguments to th function once it's evaluated). In cases A and B evaluating (calling) .Text yields another function, which call will invoke. But in case C, evaluating .Test fails because it a method of type func(string)string and there is no argument available. (Compare to {{call (.Test "hi") ''bye"}}.)
I believe you are just misunderstanding the call operation. The template you want for case C is just "{{.Test "hi"}}". The template you have is correct for cases A and B because the first argument to call evaluates (in the sense of calling the corresponding method) to a function whose type is func(string)string.
mikioh
changed the title
text/template {{call}} doesn't pass parameters to struct methods
text/template: {{call}} doesn't pass parameters to struct methods
May 2, 2015
Go version:
go version go1.4.2 darwin/amd64
OS:
OS X Yosemite Version 10.10.1
When using
{{call ...}}
in a template with a struct method the template parser fails to send any arguments to the method. This does work correctly if the method returns a function, or if the function you want to call is set to an attribute.If this isn't intended to work, the error message could be a little clearer.
This can be replicated on the Go Playground here: https://play.golang.org/p/Ka8bN3_V1f and the source from the link is below.
The text was updated successfully, but these errors were encountered: