From: Rob Pike Date: Mon, 23 Jul 2012 23:19:12 +0000 (-0700) Subject: text/template: fix bug in map indexing X-Git-Tag: go1.1rc2~2783 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ce274339a1ed46356f5322288f4ef878a06f0aab;p=gostls13.git 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 --- diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index c8a3013977..4efe2d1b38 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -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}, diff --git a/src/pkg/text/template/funcs.go b/src/pkg/text/template/funcs.go index 90fb9c52c0..e6fa0fb5f2 100644 --- a/src/pkg/text/template/funcs.go +++ b/src/pkg/text/template/funcs.go @@ -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())