URL中的参数

URL中的参数

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

    // 访问路径"*/user/John"时输出 "Hello John"
    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    // 访问路径 "/user/John/1/2" 时输出 "John is /1/2"
    router.GET("/user/:name/*action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        message := name + " is " + action
        c.String(http.StatusOK, message)
    })

    router.Run(":8080")
}

Last updated

Was this helpful?