From 7fb00911d449b9228c804fd26643cbe58aaa8792 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Thu, 15 Jun 2023 07:34:37 +0800 Subject: [PATCH] hero: optimize *binding'String For faster execution, use strconv.Itoa rather than fmt.Sprintf to convert an integer to string, remove if statement in loop. --- hero/binding.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hero/binding.go b/hero/binding.go index fbe48fe3..de1af4c1 100644 --- a/hero/binding.go +++ b/hero/binding.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "sort" + "strconv" "github.com/kataras/iris/v12/context" ) @@ -43,15 +44,14 @@ func newStructFieldInput(f reflect.StructField) *Input { // String returns the string representation of a binding. func (b *binding) String() string { - index := fmt.Sprintf("%d", b.Input.Index) + var index string if len(b.Input.StructFieldIndex) > 0 { - for j, i := range b.Input.StructFieldIndex { - if j == 0 { - index = fmt.Sprintf("%d", i) - continue - } + index = strconv.Itoa(b.Input.StructFieldIndex[0]) + for _, i := range b.Input.StructFieldIndex[1:] { index += fmt.Sprintf(".%d", i) } + } else { + index = strconv.Itoa(b.Input.Index) } return fmt.Sprintf("[%s:%s] maps to [%s]", index, b.Input.Type.String(), b.Dependency)