From 12d66f7345668b89f80e50e39a137818afbac379 Mon Sep 17 00:00:00 2001 From: daheige Date: Wed, 14 Jul 2021 22:57:08 +0800 Subject: [PATCH] lazyInit initialize the validate Settings, no need to use sync.once --- binding/default_validator.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/binding/default_validator.go b/binding/default_validator.go index 87fc4c66..a91e0496 100644 --- a/binding/default_validator.go +++ b/binding/default_validator.go @@ -8,13 +8,11 @@ import ( "fmt" "reflect" "strings" - "sync" "github.com/go-playground/validator/v10" ) type defaultValidator struct { - once sync.Once validate *validator.Validate } @@ -76,7 +74,7 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error { // validateStruct receives struct type func (v *defaultValidator) validateStruct(obj interface{}) error { - v.lazyinit() + v.lazyInit() return v.validate.Struct(obj) } @@ -85,13 +83,14 @@ func (v *defaultValidator) validateStruct(obj interface{}) error { // or struct level validations. See validator GoDoc for more info - // https://pkg.go.dev/github.com/go-playground/validator/v10 func (v *defaultValidator) Engine() interface{} { - v.lazyinit() + v.lazyInit() return v.validate } -func (v *defaultValidator) lazyinit() { - v.once.Do(func() { +// lazyInit initialize the validate Settings, no need to use sync.once +func (v *defaultValidator) lazyInit() { + if v.validate == nil { v.validate = validator.New() v.validate.SetTagName("binding") - }) + } }