golang gin ShouldBindJSON数据校验:binding:“required,min=3,max=7“ 限制字符串的长度

package main

import "github.com/gin-gonic/gin"

type User struct {
ID string `json:"id" binding:"required,email"`
Name string `json:"name" binding:"required,min=3,max=7"`
}

func main() {
r := gin.Default()
r.POST("user", func(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(200, gin.H{
"Code": 200,
"Msg": err.Error(),
})
return
}

c.JSON(200, gin.H{
"Code": 0,
"ID": user.ID,
"Name": user.Name,
})
})
r.Run()
}

文章来源于互联网:golang gin ShouldBindJSON数据校验:binding:“required,min=3,max=7“ 限制字符串的长度

THE END
分享
二维码