type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
有没有办法添加
binding:"required"
到json:"name"
而不进行硬编码?
也许您可以在句柄函数中动态覆盖结构,并且原始的Person结构保持不变。像这样:
func HandleSomething(c *gin.Context) {
type person struct {
Person
Name string `json:"name" binding:"required"`
}
var p person
if err = c.ShouldBind(&p); err != nil {
err = errors.Wrap(util.ErrParams, err.Error())
return
}
response = util.BuildResponse(middleware.PassCode, "Success", list)
c.JSON(http.StatusOK, response)
return
}