有时候参数验证需要用到类型零值,比如bool 或是标志位(0表示xxx,1表示xxx)。
这时候gin的参数验证库对struct的 json:"xxx" binding:"required"
这个tag验证是,必须带上这个参数,并且不为零值,因为不带上的话json反序列化就是零值
所以我们可以这么做
type UserInfoReq struct {
Username string `json:"username" binding:"required"`
Sex *int `json:"sex" binding:"required"`
}
这样就能真确获取想要的数据,而不会验证失败