15 【GET请求】获取路由上的参数值

package chapter02

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

// 15 【GET请求】获取路由上的参数值
// 路径中直接加上参数值
// http://localhost:9000/param/123
func Param1(ctx *gin.Context){
	id := ctx.Param("id")
	ctx.String(http.StatusOK,"hello %s",id)  // hello 123
}

// 16【GET请求】query获取get请求参数
// http://localhost:9000/query?name=lin
//http://localhost:9000/query?name=lin&age=32
func GetQuestData(ctx *gin.Context){
	name := ctx.Query("name")
	age := ctx.DefaultQuery("age","18")  // 多了默认值
	ctx.String(http.StatusOK,"hello %s age is %s",name,age)  // hello lin
}


上一篇:Gin基本使用


下一篇:[Go] gin 解决: accept4: too many open files