1. 代码:

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

2. 解释

可以获取 /user/....后面的路径名字:

  • 第 1 段代码

image.png

  • 第 2 段代码

image.png