Files
Go-StudyExample/example/gin/32-RouterMatch_test.go
T

43 lines
937 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author : huangzj
* @Time : 2020/12/8 15:54
* @Description
*/
package gin
import (
"github.com/gin-gonic/gin"
"net/http"
"testing"
)
func TestRouterMatch(t *testing.T) {
router := gin.Default()
OnlyMatch(router) //只能匹配到指定格式
MathMany(router) //*匹配多重字段
_ = router.Run(":8080")
}
func MathMany(router *gin.Engine) {
// 此 handler 将匹配 /user/john/ 和 /user/john/send
// 如果没有其他路由匹配 /user/john,它将重定向到 /user/john/
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)
})
}
func OnlyMatch(router *gin.Engine) {
// 此 handler 将匹配 /user/john 但不会匹配 /user/ 或者 /user
router.GET("/user/:name", func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s", name)
})
}