参数验证
PPG007 ... 2021-12-26 Less than 1 minute
# 参数验证
# 结构体验证
type Person struct {
// 大于10
Age int `form:"age" binding:"required,gt=10"`
Name string `form:"name" binding:"required"`
// 限制格式
Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`
}
func main() {
router := gin.Default()
router.POST("/demo", func(c *gin.Context) {
var person Person
if err :=c.ShouldBind(&person); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"err": err})
return
}
c.JSON(http.StatusOK, person)
})
router.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21