-
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: index is too restrictive in its type validation #15974
Comments
Just like in #14751, index mirrors ordinary Go execution. It doesn't just take anything and do its best. This is not, say, Javascript. |
@rsc in this case it does definitely not mirror Go execution. m["Key"] // Ok! Is different than: {{ index . "Key" }} // Panic! I understand your resistance to change existing behavior in Go, but please don't make up arguments that doesn't hold water.
|
@bep you wrote
but they cannot. Only
In this template code:
k := "Key"
_ = m["Key"] you'd get the equivalent compile error: https://play.golang.org/p/wdLJzrtl_p All that said, I agree that the template code is necessarily more clumsy than Go code because it lacks the convenience of constants. I'm not sure anything can be done to improve the situation in a backwards-compatible manner, though. |
Any way you spint it: given my latest examples |
Given:
This panics:
Juggling around with
template.HTML
andstring
types seems like a common thing in Go templates, and I would expect theindex
func to work the same way as a straightmap
lookup in this case.The text was updated successfully, but these errors were encountered: