【问题标题】:How to define a go-gin route with an id in the middle如何定义中间有id的go-gin路由
【发布时间】:2016-09-14 20:16:10
【问题描述】:

我要定义路线

/user/{userid}/status

如何定义这种路由并在处理程序中截取用户 ID。像这样的

 r.GET("/user/{userid}/status", userStatus)

在这种情况下如何读取我的 Go 代码中的 userid 变量?

【问题讨论】:

    标签: go go-gin


    【解决方案1】:

    您可以使用userid := c.Param("userid"),就像这个工作示例:

    package main
    
    import (
        "fmt"
        "net/http"
    
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        router := gin.Default()
    
        router.GET("/user/:userid/status", func(c *gin.Context) {
            userid := c.Param("userid") 
            message := "userid is " + userid
            c.String(http.StatusOK, message)
            fmt.Println(message)
        })
    
        router.Run(":8080")
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多