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
package main
import (
"html/template""os"
)
funcmain() {
m:=make(map[template.HTML]string)
m["Key"] ="Val"// OK!if_, ok:=m["Key"]; !ok {
panic("Map lookup failed")
}
t, err:=template.New("foo").Parse(data)
iferr!=nil {
panic(err)
}
// This failserr=t.Execute(os.Stdout, m)
iferr!=nil {
panic(err)
}
}
vardata=`{{ index . "Key" }}`
This panics:
panic: template: foo:1:3: executing "foo" at <index . "Key">: error calling index: value has type string; should be template.HTML
Juggling around with template.HTML and string types seems like a common thing in Go templates, and I would expect the index func to work the same way as a straight map lookup in this case.
The text was updated successfully, but these errors were encountered:
@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.
template.HTML is a string -- and when strings can be used as keys in map[template.HTML] lookups, then the same should be the case for the index func. A template.HTML isn't anything, it is a well defined string.
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 index func will not win any usability price awards. The "cannot fix because it would break" is an argument I'll happily buy, but I just get slightly annoyed by these this is as designed arguments.
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: