Parameters in path

  1. func main() {
  2. router := gin.Default()
  3. // This handler will match /user/john but will not match /user/ or /user
  4. router.GET("/user/:name", func(c *gin.Context) {
  5. name := c.Param("name")
  6. c.String(http.StatusOK, "Hello %s", name)
  7. })
  8. // However, this one will match /user/john/ and also /user/john/send
  9. // If no other routers match /user/john, it will redirect to /user/john/
  10. router.GET("/user/:name/*action", func(c *gin.Context) {
  11. name := c.Param("name")
  12. action := c.Param("action")
  13. message := name + " is " + action
  14. c.String(http.StatusOK, message)
  15. })
  16. router.Run(":8080")
  17. }

Last modified March 7, 2020 : add blog dir (#115) (f46734b)