今天测试我post接口的时候,发现一个接收入参问题,我把一个参数设置默认值为0,int类型,但是如果传的是0 。 进行入参序列化的时候就报required 错误具体如下:
Key: 'RuleListReq.Status' Error:Field validation for 'Status' failed on the 'required' tag
type RuleListReq struct {
RuleType int `json:"rule_type,omitempty"`
Name string `json:"name,omitempty"`
Status int `json:"status" binding:"required"` // 要让前端默认值传 -1 必传值
Operator string `json:"operator,omitempty"`
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
}
func (sc *SensitiveConfigure) RuleList(c *gin.Context) {
var form RuleListReq
// 接参
if err := c.Bind(&form); err != nil {
sc.BadResponse(10000, err.Error(), c)
return
}
db := providers.MysqlMb4
data, total, err := sc.sensitiveRuleDao.RuleList(db, form)
if err != nil {
sc.BadResponse(10000, err.Error(), c)
return
}
sc.JsonPageResponse(data, total, c)
}
{
"code": 10000,
"msg": "参数错误",
"data": "Key: 'RuleListReq.Status' Error:Field validation for 'Status' failed on the 'required' tag"
}
首先我按照网上的百度和其他人解决这个问题的 几乎都是将Status int 类型改成Status *int于是我也将int改成了*int 但是请求后的结果依然还是报这个错误。
是为什么还是报这个错误呢,我之前好奇也在错误中打印了一下 form的 %+v的值为:
form值:{RuleType:0 Name: Status:0xc0002e0ab0 Operator: Page:1 PageSize:10}
为什么还是会有这个问题呢?