By Badis Merabet


2019-12-02 22:19:22 8 Comments

I using https://github.com/gin-gonic/gin and I am trying to implement nested routes like this:

// 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
}

but I am getting weird errors:

[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

and I am not able to find a satisfying solution in or outside SO.

If you want to reproduce issue quickly, take a look here: https://github.com/badis/so-question-gin-routes

0 comments

Related Questions

Sponsored Content

1 Answered Questions

Gin router path conflict

  • 2016-04-01 13:37:10
  • Xeph
  • 2067 View
  • 2 Score
  • 1 Answer
  • Tags:   go go-gin

2 Answered Questions

[SOLVED] Gin wildcard route conflicts with existing children

  • 2018-10-17 00:44:48
  • maerics
  • 2089 View
  • 2 Score
  • 2 Answer
  • Tags:   web-services go gin

4 Answered Questions

[SOLVED] Go: panic: runtime error: invalid memory address or nil pointer dereference

  • 2013-04-29 13:41:16
  • elithrar
  • 145832 View
  • 79 Score
  • 4 Answer
  • Tags:   go

1 Answered Questions

1 Answered Questions

[SOLVED] golang gin-gonic and splitting files in package

1 Answered Questions

[SOLVED] Setting up Route Not Found in Gin

  • 2015-09-07 17:41:08
  • tommyd456
  • 7943 View
  • 13 Score
  • 1 Answer
  • Tags:   go go-gin

1 Answered Questions

[SOLVED] Panic error when trying to serve a Gin Gonic app

  • 2016-11-26 00:21:45
  • jmm
  • 538 View
  • 0 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Servicestack conflicting routes?

Sponsored Content