如何在 Gin 中获取上下文中的匹配路线?

nin*_*ing 3 go go-gin

我有这个代码:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.New()
    r.GET("/user/:id", func(c *gin.Context) {
        // How can I get the litteral string "/user/:id" here ?
        c.JSON(http.StatusOK, gin.H{"message": "received request"})
    })
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在处理程序内部检索垃圾字符串吗/user/:id?如果我使用c.Request.Path它会给我路径的完整输出,例如/user/10.

Sim*_* S. 7

根据文档,您可以使用FullPath()

router.GET("/user/:id", func(c *gin.Context) {
    c.FullPath() == "/user/:id" // true
})
Run Code Online (Sandbox Code Playgroud)