Gin 使用示例(二十五):URL 路径中的参数


可选参数、必填参数示例代码:

func main() {
    router := gin.Default()
    
    // This handler will match /user/xueyuanjun but will not match /user/ or /user
    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })
    
    // However, this one will match /user/xueyuanjun/ and also /user/xueyuanjun/send
    // If no other routers match /user/xueyuanjun, it will redirect to /user/xueyuanjun/
    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")
}

运行上述代码,在终端窗口通过 curl 发起模拟请求进行测试:

-w724


Vote Vote Cancel Collect Collect Cancel

<< 上一篇: Gin 使用示例(二十四):只绑定查询字符串

>> 下一篇: Gin 使用示例(二十六):PureJSON