go语言 gin框架学习笔记(二)之 简单传参

gin 传参方式可以采用反斜杠+冒号(/:

采用该方法传参,前台请求服务时,必须传指定数量的参数(不能多传也不能少传)否则请求失败


代码展示

package main

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

func main() {
   router :=gin.Default()
   // 前台请求时必须传入两个参数第一个值被赋值到name变量 第二个值被赋值到phone变量,否则请求失败
   router.GET("/:name/:phone", func(c *gin.Context) {
      name := c.Param("name")
      phone := c.Param("phone")
      c.String(http.StatusOK,"the phone of %s is %s " ,name,phone)
   })
   router.Run(":1688")
}

正确的传参

多传参数

少传参数

猜你喜欢

转载自blog.csdn.net/SeaLong_/article/details/88844663
今日推荐