【Gin框架】Gin参数绑定

package main

import (
   "fmt"
   "github.com/gin-gonic/gin"
   "net/http"
)
type UserInfo struct {
   Username string `form:"username"`
   Password string `form:"password"`
}
func main() {
   r := gin.Default()

   r.POST("/form", func(c *gin.Context) {
      var u UserInfo
      // 参数绑定
      err := c.ShouldBind(&u)
      if err != nil {
         c.JSON(http.StatusBadRequest, gin.H{
            "error: ": err.Error(),
         })
      } else {
         fmt.Println("%#v\n", u)
         c.JSON(http.StatusOK, gin.H{
            "status": "ok",
         })
      }
   })

   r.Run()
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/112712324