【发布时间】:2020-03-27 13:43:37
【问题描述】:
我使用https://github.com/gin-gonic/gin,我正在尝试实现这样的嵌套路由:
// SetupRouter initialize routes and handlers
func SetupRouter() *gin.Engine {
r := gin.Default()
r.GET("/", controllers.Welcome)
r.GET("/resources", controllers.GetResources)
r.GET("/resources/otherroute", controllers.GetOResources)
r.GET("/resources/:id", controllers.GetResourcesByID)
r.GET("/resources/:id/sub-resources", GetSubResources)
r.GET("/resources/:id/sub-resources/:srid", GetSubResourcesByID)
r.GET("/resources/:id/sub-resources/:srid/ssub-resources", GetSSubResources)
r.GET("/resources/:id/sub-resources/:srid/ssub-resources/:ssrid", GetSSubResourcesByID)
// .... etc.
return r
}
但我遇到了奇怪的错误:
[GIN-debug] GET / --> github.com/badis/so-
gin-nested-routes/controllers.Welcome (3 handlers)
[GIN-debug] GET /resources --> github.com/badis/so-gin-nested-routes/controllers.GetResources (3 handlers)
[GIN-debug] GET /resources/otherroute --> github.com/badis/so-gin-nested-routes/controllers.GetOResources (3 handlers)
[GIN-debug] GET /resources/:id --> github.com/badis/so-gin-nested-routes/controllers.GetResourcesByID (3 handlers)
panic: wildcard route ':id' conflicts with existing children in path '/resources/:id'
goroutine 1 [running]:
github.com/gin-gonic/gin.(*node).insertChild(0xc0002516c0, 0xc0001edc01, 0xc0002325eb, 0x3, 0xc0002325e0, 0xe, 0xc000224300, 0x3, 0x3)
/home/badis/go/src/github.com/gin-gonic/gin/tree.go:294 +0x807
github.com/gin-gonic/gin.(*node).addRoute(0xc0002516c0, 0xc0002325e0, 0xe, 0xc000224300, 0x3, 0x3)
/home/badis/go/src/github.com/gin-gonic/gin/tree.go:255 +0x4ea
我无法在 SO 内部或外部找到令人满意的解决方案。
如果你想快速重现问题,请看这里:https://github.com/badis/so-question-gin-routes
【问题讨论】:
-
我相信您不能将动态细分与静态细分混为一谈。您可以使用
/resources/otherroute或/resources/:id,但不能同时使用。
标签: rest go routing web-frameworks