6

I want to define a route

/user/{userid}/status

How can I define this kind of route and intercept the userid in handler. Something like this

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

How can read the userid variable in my Go code in such case?

codec
  • 7,978
  • 26
  • 71
  • 127

1 Answers1

22

You may use userid := c.Param("userid"), like this working sample:

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")
}