document thread safety and recommended singleton usage (#809)

pull/816/head
András Czigány 3 years ago committed by GitHub
parent a67baa74f1
commit 42525d89ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      doc.go
  2. 4
      validator_instance.go

@ -7,6 +7,14 @@ and has the ability to dive into arrays and maps of any type.
see more examples https://github.com/go-playground/validator/tree/master/_examples see more examples https://github.com/go-playground/validator/tree/master/_examples
Singleton
Validator is designed to be thread-safe and used as a singleton instance.
It caches information about your struct and validations,
in essence only parsing your validation tags once per struct type.
Using multiple instances neglects the benefit of caching.
The not thread-safe functions are explicitly marked as such in the documentation.
Validation Functions Return Type error Validation Functions Return Type error
Doing things this way is actually the way the standard library does, see the Doing things this way is actually the way the standard library does, see the

@ -89,6 +89,10 @@ type Validate struct {
} }
// New returns a new instance of 'validate' with sane defaults. // New returns a new instance of 'validate' with sane defaults.
// Validate is designed to be thread-safe and used as a singleton instance.
// It caches information about your struct and validations,
// in essence only parsing your validation tags once per struct type.
// Using multiple instances neglects the benefit of caching.
func New() *Validate { func New() *Validate {
tc := new(tagCache) tc := new(tagCache)

Loading…
Cancel
Save