Route Groups
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:
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:
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:
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:
api := r.Group("/api", authMiddleware)
api.DELETE("/users/{id}", auditLog, deleteUser)
// runs authMiddleware + auditLog for this route only