I was wondering how to get a Go Gin-Gonic Web Server to handle Not Found Routes for a specific Group. If I have the following Code :
func main() {
r := gin.Default()
// handle /sub routes
rGroup := r.Group("/sub")
{
rGroup.GET("/a", func(c *gin.Context) {
// respond found /sub/a
})
rGroup.NoRoute(func(c *gin.Context) { // this fails, NoRoute is not available
// respond not found in /sub
})
}
// handle / routes
r.GET("/a", func(c *gin.Context) {
// respond found /a
})
r.NoRoute(func(c *gin.Context) {
// respond not found in /
})
r.Run(":8000")
}
Then it is working for the / routes but the Group does not have the NoRoute Method so is there another way to achieve the same thing?
After conducting some research, it appears that Gin currently does not support this directly. Instead, it can be achieved in a different way using
NoRouteHere is a sample implementation; I hope this helps.
Reference :