Skip to main content
A route group lets you collect related routes under a common URL prefix and attach middleware that runs only for those routes. You create a group with app.Group(prefix) and register routes on the returned *Group value exactly as you would on the application.

Creating a group

The *Group type exposes the same HTTP method shortcuts as *Application: Get, Post, Put, Patch, Delete, Head, Options, and AddRoute.

Adding middleware to a group

Call group.Use() to register middleware that runs only for routes in that group. Global middleware registered on the application still runs first.
The execution order for GET /api/profile:
A request to a route outside the group (e.g. GET /ping) skips the group middleware entirely.

Nested groups

Call group.Group(prefix) to create a child group that inherits the parent’s prefix and middleware.
Middleware is additive: a route in a nested group runs the global middleware, then the parent group’s middleware, then the child group’s middleware, and finally the route handler.

Complete example

The example below builds an /api/v1 group with authentication applied to all routes, and a nested /api/v1/admin group with an additional admin-only check.
Request flow for GET /api/v1/users:
Request flow for GET /api/v1/admin/stats:
Group middleware is cached after the first route registration on a group. Call group.Use() before registering any routes to ensure all middleware is included.

Using AddRoute on a group

When you need to register a route for a method determined at runtime, group.AddRoute works the same as app.AddRoute but prepends the group prefix and group middleware.