Golang Go语言中 gin 条件验证请教下
Golang Go语言中 gin 条件验证请教下
package main
import (
“fmt”
“github.com/gin-gonic/gin”
“net/http”
)
type Response struct {
Code int json:"code"
Msg string json:"msg"
Data interface{} json:"data"
}
type User struct {
Id int json:"id"
Phone string json:"phone"
}
type LoginParams struct {
Phone int64 binding:"required,min=11,max=11" form:"phone" json:"phone"
}
func ReturnResponse(c *gin.Context, code int, msg string, data interface{}) {
c.JSON( http.StatusOK, Response{
Code: code,
Msg: msg,
Data: data,
})
}
func RegisterOrLogin(c *gin.Context) {
var loginParams LoginParams
var user User
err := c.ShouldBind(&loginParams)
if err != nil {
ReturnResponse(c, 40040, err.Error(), nil)
return
}
result := MysqlDb.Table(“user”).Find(&user)
fmt.Println(result)
ReturnResponse(c, 200, “success”, result)
}
验证请求参数 phone 最开始设置 len=11,查看文档发现,在 int 情况下,直接验证这个数值。 换成 min,max 也是验证这个数值 如果我验证 int 长度应该用什么
更多关于Golang Go语言中 gin 条件验证请教下的实战教程也可以访问 https://www.itying.com/category-94-b0.html