7days-golang
7days-golang copied to clipboard
url动态参数与固定参数冲突
// 1
r.GET("/hello/cool", func(c *gee.Context) {
c.String(http.StatusOK, "cool cool, you're at %s", c.Path)
})
r.GET("/hello/:name", func(c *gee.Context) {
// expect /hello/geektutu
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
})
//2
r.GET("/hello/:name", func(c *gee.Context) {
// expect /hello/geektutu
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
})
r.GET("/hello/cool", func(c *gee.Context) {
c.String(http.StatusOK, "cool cool, you're at %s", c.Path)
})
上面两种设置路由的形式会得到不同的效果:
1的方式可以正常访问/hello/cool
和/hello/:name
.
2的形式,当设置/hello/cool
的时候会将原来的:name
节点的pattern换成/hello/cool
, 导致后续/hello/:name
无法继续正常工作。
修改改的方式就是:
添加cool这个节点的时候新建节点。并且将动态参数的优先级降低(排到数组的后面)。