示例如下,详细用法见 https://gin-gonic.com/zh-cn/docs/
test.go
package main
// ShouldBind学习,验证和绑定
import (
"github.com/gin-gonic/gin"
"time"
)
// 多个用,隔开,并列用|
type Boy struct{
Name string `form:"name" binding:"required"`
Age int `form:"age" binding:"required,gt=0"`
Address string `form:"address"`
Brithday time.Time `form:"brithday" time_format:"2006-01-02"`
}
func handleBoy(c *gin.Context) {
var boy Boy
if err := c.ShouldBind(&boy); err == nil {
//c.String(200, "%v", boy)
c.JSON(200, gin.H{
"name": boy.Name,
"age": boy.Age,
"address": boy.Address,
"brithday": boy.Brithday,
})
} else {
c.JSON( 200, gin.H{
"error": err.Error(),
})
//c.String(200, "%v", err)
}
}
func main(){
r :