From d15268eb22bd24c83c10819e509d1cf7a970f049 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 20 Jun 2019 00:04:06 +0800 Subject: [PATCH] improve codes --- g/util/gvalid/gvalid_check_map.go | 7 ++++--- g/util/gvalid/gvalid_check_struct.go | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/g/util/gvalid/gvalid_check_map.go b/g/util/gvalid/gvalid_check_map.go index ed607a3bf..88a450476 100644 --- a/g/util/gvalid/gvalid_check_map.go +++ b/g/util/gvalid/gvalid_check_map.go @@ -7,8 +7,9 @@ package gvalid import ( - "github.com/gogf/gf/g/util/gconv" "strings" + + "github.com/gogf/gf/g/util/gconv" ) // 检测键值对参数Map, @@ -75,7 +76,7 @@ func CheckMap(params interface{}, rules interface{}, msgs ...CustomMsg) *Error { } } // 开始执行校验: 以校验规则作为基础进行遍历校验 - value := (interface{})(nil) + var value interface{} // 这里的rule变量为多条校验规则,不包含名字或者错误信息定义 for key, rule := range checkRules { // 如果规则为空,那么不执行校验 @@ -92,7 +93,7 @@ func CheckMap(params interface{}, rules interface{}, msgs ...CustomMsg) *Error { if value == nil || gconv.String(value) == "" { required := false // rule => error - for k, _ := range item { + for k := range item { if _, ok := mustCheckRulesEvenValueEmpty[k]; ok { required = true break diff --git a/g/util/gvalid/gvalid_check_struct.go b/g/util/gvalid/gvalid_check_struct.go index 9ad4e39df..e2750ad3d 100644 --- a/g/util/gvalid/gvalid_check_struct.go +++ b/g/util/gvalid/gvalid_check_struct.go @@ -7,10 +7,11 @@ package gvalid import ( + "strings" + "github.com/gogf/gf/g/text/gstr" "github.com/gogf/gf/g/util/gconv" "github.com/gogf/gf/third/github.com/fatih/structs" - "strings" ) // 校验struct对象属性,object参数也可以是一个指向对象的指针,返回值同CheckMap方法。 @@ -121,7 +122,7 @@ func CheckStruct(object interface{}, rules interface{}, msgs ...CustomMsg) *Erro /* 以下逻辑和CheckMap相同 */ // 开始执行校验: 以校验规则作为基础进行遍历校验 - value := (interface{})(nil) + var value interface{} // 这里的rule变量为多条校验规则,不包含名字或者错误信息定义 for key, rule := range checkRules { value = nil @@ -134,7 +135,7 @@ func CheckStruct(object interface{}, rules interface{}, msgs ...CustomMsg) *Erro if value == nil || gconv.String(value) == "" { required := false // rule => error - for k, _ := range item { + for k := range item { if _, ok := mustCheckRulesEvenValueEmpty[k]; ok { required = true break