From 95fc7031a7f0efab17cf7933a896cc205d201b8c Mon Sep 17 00:00:00 2001 From: cloud Date: Wed, 3 Jun 2026 13:08:25 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=9AtagValue=20only=20needs=20to=20be=20a?= =?UTF-8?q?ssigned=20once,=20which=20removes=20an=20unnecessary=20step.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2:This reduces a redundant check. When tagValue is empty, we can first check whether field.Name is empty before assigning it.With this approach, the comment in the second block can also be removed. 3:The second invocation of k, v := head(opt, "=") in the original code appears to be unnecessary and can be removed. Some of these changes may be too minor to provide any meaningful benefit, and the actual optimization is very limited. Please feel free to ignore any suggestions that do not seem worthwhile. --- binding/form_mapping.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index c42fa997..836bef00 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -143,23 +143,22 @@ type setOptions struct { } func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) { - var tagValue string var setOpt setOptions tagValue, opts := head(field.Tag.Get(tag), ",") if tagValue == "" { // default value is FieldName + if field.Name == "" { + return false, nil + } tagValue = field.Name } - if tagValue == "" { // when field is "emptyField" variable - return false, nil - } var opt string for len(opts) > 0 { opt, opts = head(opts, ",") - - if k, v := head(opt, "="); k == "default" { + k, v := head(opt, "=") + if k == "default" { setOpt.isDefaultExists = true setOpt.defaultValue = v @@ -170,7 +169,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter setOpt.defaultValue = strings.ReplaceAll(v, ";", ",") } } - } else if k, v = head(opt, "="); k == "parser" { + } else if k == "parser" { setOpt.parser = v } }