-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: pointer receivers methods do not work with non pointer values in templates #35079
Comments
Thanks for the issue report. Issue #18816 may be somewhat relevant.
I see that you're using slices in template 3 and 4. Did you mean to say slice instead of array?
I think there's an inconsistency in that you're using a local variable d := Data{Field: "test"} That means when you write
Take a look at https://play.golang.org/p/qsyqsjW3i7Z. If you change it to use a
If you apply the changes above to the original playground snippet, then everything looks consistent: |
I did mean to say slice.
That clears up the confusion. Thank you. I looked into it a little more and it seems it does not work in the template because the reflect package can not address the value in the template (ptr.CanAddr() returns false) - which makes sense as it is not a pointer. I don't know if this behavior should be changed, and if so, how. |
Glad to hear that.
That's right, the value needs to be addressable. I don't think anything can or should be changed in I'll close this since there's nothing to do here. Let me know if you disagree. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I tried to call a method with a pointer reciever on a non pointer value in a template.
Here is an example: https://play.golang.org/p/GRqFTgTCwZh
What did you expect to see?
That the call to the method with a pointer reciever would behave the same in the template as in regular Go syntax. I should be able to call the method on a non pointer value.
What did you see instead?
The template execution errored out with the
error template: :1:2: executing "" at <.PrintPointer>: can't evaluate field PrintPointer in type main.Data
Calling the method with a pointer reciever does work when the item is in an array.
The text was updated successfully, but these errors were encountered: