From 1ed85ee228023d766b37db056311929c00091c9f Mon Sep 17 00:00:00 2001 From: Joe Taber Date: Mon, 18 Mar 2024 05:52:30 +0000 Subject: [PATCH] text/template: simplify unwrapping reflect.Interface value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When text/template is evaluating a pipeline command and encounters an `interface{}`, it "digs down one level to the thing inside". Currently it does this with `value = reflect.ValueOf(value.Interface())`, which is unnecessary since it could just use `value = value.Elem()`. This commit changes it to use the latter. Why it was written that way is mysterious because the proposed change appears to be strictly better, but given the blame date (13 years ago) it may have been written while reflect was still in development before `Elem()` was added. Change-Id: I6c4f6283e78de07732c4120ce11f26f113fa46e4 GitHub-Last-Rev: bdfc6973ab227f951f244fda4d803da55fb49e71 GitHub-Pull-Request: golang/go#66373 Reviewed-on: https://go-review.googlesource.com/c/go/+/572355 Auto-Submit: Ian Lance Taylor Reviewed-by: Rob Pike LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Ian Lance Taylor Reviewed-by: Daniel Martí Reviewed-by: Emmanuel Odeke --- src/text/template/exec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/text/template/exec.go b/src/text/template/exec.go index 20d8f98f28..4c899b1c79 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -479,7 +479,7 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref value = s.evalCommand(dot, cmd, value) // previous value is this one's final arg. // If the object has type interface{}, dig down one level to the thing inside. if value.Kind() == reflect.Interface && value.Type().NumMethod() == 0 { - value = reflect.ValueOf(value.Interface()) // lovely! + value = value.Elem() } } for _, variable := range pipe.Decl {