go语言 gin框架学习笔记(四)之 post请求与参数默认值

post 请求时是通过PostForm获取前端body中传入的参数

可以使用DefaultPostForm为参数设置一个默认值,当前端没有传参时直接默认值赋值给相应的参数


代码展示
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

func main() {
   router :=gin.Default()
   router.POST("/", func(c *gin.Context) {
      // code 默认值的默认值为200
      msg  := c.PostForm("msg")
      code := c.DefaultPostForm("code","2001")
      name :=c.PostForm("name")
      c.String(http.StatusOK,"code = %s msg = %s name = %s" ,code,msg,name)
   })
   router.Run(":1688")
}

界面展示

全部参数都传值时

设置了默认参数的参数,传空值时的界面展示

猜你喜欢

转载自blog.csdn.net/SeaLong_/article/details/88872106