常用的开源包validator:
import gopkg.in/go-playground/validator.v8
import gopkg.in/go-playground/validator.v9
基础用法参看文档即可:
https://godoc.org/gopkg.in/go-playground/validator.v8
https://godoc.org/gopkg.in/go-playground/validator.v9
一点trick的用法:
由于golang的特性,结构体基础数据类型没有赋值会默认零值(int默认0,string默认""等),所以require不能校验出基础类型是默认零值,还是被赋为了零值。
比如:
CommType int64 `json:"comm_type"validate:"exists"`
这样无法判断是传入了0表示某种商品类型,还是根本就没传,一种解决办法是: