7days-golang icon indicating copy to clipboard operation
7days-golang copied to clipboard

url动态参数与固定参数冲突

Open 0xff-dev opened this issue 4 years ago • 0 comments

// 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这个节点的时候新建节点。并且将动态参数的优先级降低(排到数组的后面)。

0xff-dev avatar Feb 21 '21 16:02 0xff-dev