Skip to content

Route Groups ​

Added: v1.0.0

Route groups let you organize related endpoints under a common path prefix with shared middleware.

Basic Group ​

Create a group with a path prefix. All routes inside inherit the prefix:

go
api := r.Group("/api")
api.GET("/health", healthCheck)
// handles GET /api/health

With Middleware ​

Middleware applied to a group runs for every route in that group:

go
admin := r.Group("/admin", authMiddleware, auditLog)
admin.GET("/dashboard", dashboard)
admin.POST("/users", createUser)
// both routes run authMiddleware + auditLog

Nested Groups ​

Groups can be nested. Inner groups inherit the prefix and middleware from their parent:

go
admin := r.Group("/admin", adminMiddleware)
users := admin.Group("/users")
users.GET("/{id}", getUser)
// handles GET /admin/users/{id} with adminMiddleware

Per-Route Middleware ​

Add extra middleware to individual routes within a group:

go
api := r.Group("/api", authMiddleware)
api.DELETE("/users/{id}", auditLog, deleteUser)
// runs authMiddleware + auditLog for this route only

Released under the MIT License.