gin中的路由参数

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	// 路由参数1,将匹配/user/mayanan和/user/,但是不会匹配/user
	//router.GET("/user/:name", func(context *gin.Context) {
	//	name := context.Param("name")
	//	context.String(200, name)
	//})

	// 路由参数2 将匹配 /user/mayanan/和/user/mayanan/abcdef
	// 不能匹配 /user/和/user/mayanan
	router.GET("/user/:name/*action", func(context *gin.Context) {
		name := context.Param("name")
		action := context.Param("action")
		fmt.Printf("name=%s, action=%s\n", name, action)
		context.JSON(200, gin.H{"name": name, "action": action})
	})

	router.Run()
}

  

posted @ 2021-10-28 15:54  专职  阅读(349)  评论(0编辑  收藏  举报