From 402d6b1f5fbbe19d462ea38b0c4cbf699dfda5e6 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 22 Sep 2012 05:54:11 +1000 Subject: [PATCH] [release-branch.go1] text/template: fix bug in map indexing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit ««« 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 »»» --- src/pkg/text/template/exec_test.go | 2 +- src/pkg/text/template/funcs.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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()) -- 2.50.0