From: Rob Pike Date: Fri, 21 Sep 2012 19:54:11 +0000 (+1000) Subject: [release-branch.go1] text/template: fix bug in map indexing X-Git-Tag: go1.0.3~143 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=402d6b1f5fbbe19d462ea38b0c4cbf699dfda5e6;p=gostls13.git [release-branch.go1] text/template: fix bug in map indexing ««« backport 0748cd92ed76 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 f4ae50f0ee..64149533b3 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -387,7 +387,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())