1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:34:39 -07:00

text/template: fix bug in map indexing

If the key is not present, return value of the type of the element
not the type of the key. Also fix a test that should have caught this case.

Fixes #3850.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/6405078
This commit is contained in:
Rob Pike 2012-07-23 16:19:12 -07:00
parent 7b73251d3d
commit ce274339a1
2 changed files with 2 additions and 2 deletions

View File

@ -390,7 +390,7 @@ var execTests = []execTest{
{"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false},
{"map[one]", "{{index .MSI `one`}}", "1", tVal, true},
{"map[two]", "{{index .MSI `two`}}", "2", tVal, true},
{"map[NO]", "{{index .MSI `XXX`}}", "", tVal, true},
{"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true},
{"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
{"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},

View File

@ -128,7 +128,7 @@ func index(item interface{}, indices ...interface{}) (interface{}, error) {
if x := v.MapIndex(index); x.IsValid() {
v = x
} else {
v = reflect.Zero(v.Type().Key())
v = reflect.Zero(v.Type().Elem())
}
default:
return nil, fmt.Errorf("can't index item of type %s", index.Type())