Check
go
func Check(v interface{}, lang ...language.Tag) (errs []*ErrContext, ok bool)Validate v against its struct tags and (optionally) its Validate() error method. Returns the list of failures and an ok flag that's true only when no failures occurred.
Inputs
| Param | Notes |
|---|---|
v | Struct, struct pointer, or slice of structs. Other kinds (maps, ints, …) return (nil, true). |
lang | Optional locale. First value wins; the rest are ignored. Defaults to language.Chinese. |
Behavior
- Nil-safe.
niland typed-nil pointers return(nil, true). - Pointer-aware. Pointers are dereferenced; the original value's method set is captured first so
Validate()works whether you pass the value or a pointer. - Recursive. Nested structs and slices of structs are walked automatically (see Nested Structs & Slices).
- Tag-driven. Each rule from
valid:"…"runs against the field's value, usingCheckersto resolve the checker function. - Validate hook. After tag rules, if
v(or its pointer) has aValidate() error, govalid invokes it and appends any non-nil error.
Examples
Basic
go
errs, ok := govalid.Check(form)Locale per call
go
errs, ok := govalid.Check(form, language.English)Slice of structs
go
items := []Item{{Name: ""}, {Name: "ok"}}
errs, ok := govalid.Check(items)Nil pointer
go
var f *Form
errs, ok := govalid.Check(f) // returns (nil, true)Return value
errs is a slice of *ErrContext, each implementing error. Order is:
- Top-level field declaration order.
- Within a slice-of-structs field, element order.
- Within an element, field declaration order.
Validate()errors come last.