用gin框架的数据验证,可以不用解析数据,减少if else,会简洁许多。
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
)
// 初始化一个cookie存储对象
// something-very-secret应该是一个你自己的密匙,只要不被别人知道就行
var store = sessions.NewCookieStore([]byte("secret"))
type Person struct {
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() {
r := gin.Default()
r.GET("/51mh", func(ctx *gin.Context) {
var person Person
if err := ctx.ShouldBind(&person); err != nil {
ctx.String(500, fmt.Sprint(err))
return
}
ctx.String(200, fmt.Sprintf("%#v", person))
})
r.Run()
}
结果是:感觉没有经过验证