New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
text/template: calling a *T method on elements of []T: works with range, not with index #14916
Comments
This is text/template too just FYI. |
Suppose dot is a []T with len==1 and there is a (*T) M() method (pointer receiver). The problem is that the implementation of range ends up trying
while the use of index induces a conversion from reflect.Value to interface{} and back, so
That round trip makes x not addressable, so MethodByName cannot do an implicit address-of, so it cannot get at the pointer-receiver method M. The options I see are:
Here is the diff for 2 (and then the obvious changes to func index to make it take and return reflect.Value instead of interface{}):
Here is the diff for 3:
Thoughts? |
Makes sense to me. I have trouble reading diffs without context like this the but the theory behind option 2 sounds good and the excerpts seem OK. Please make a CL. |
CL https://golang.org/cl/31462 mentions this issue. |
go version
)?1.6
go env
)?linux/amd64
http://play.golang.org/p/5EinJM-QLi
Consider a template where
dot
is set to a slice of (non-pointer) values.If I
{{range}}
ondot
, I can call pointer-receiver methods on each element. However, if I{{index}}
ondot
, then pointer-receiver methods cause the template execution to fail with a "PointerReceiver is not a field of struct type main.someStruct" error.Pointer-receiver method works on
{{index}}
ed elements just as it does on{{range}}
d ones.It has been suggested to me that might be "a bug in the template package: for method calls on slice elements, it should probably be using
v.Index(i).Addr().Call([...])
rather thanv.Index(i).Call([...])
"See error above.
The text was updated successfully, but these errors were encountered: