Group
Group is Zincβs route subtree type.
Create one with:
api := app.Group("/api", requireAPIKey)
What groups doβ
Groups let you:
- apply a common path prefix
- apply a shared middleware chain
- build nested APIs without repeating paths
Common methodsβ
| Method | Purpose |
|---|---|
Use | Add group-local middleware |
Group, Route | Create nested route trees |
Get, Post, Put, Patch, Delete, Head, Options, Connect, Trace | Register routes under the group prefix |
Add, Match, All, Any | Register more general route sets |
Handle | Register a named route with RouteSpec |
RouteNotFound | Register a prefix-scoped not-found route |
Mount, Static, StaticFS, File, FileFS | Mount handlers or serve files below the group prefix |
Exampleβ
api := app.Group("/api", requireAPIKey)
v1 := api.Group("/v1")
v1.Get("/users/:id", showUser)
v1.Post("/users", createUser)
if err := v1.Handle(zinc.RouteSpec{
Name: "users.show",
Method: zinc.MethodGet,
Path: "/users/:id",
Handler: showUser,
}); err != nil {
log.Fatal(err)
}
Group route registration inherits the group prefix and middleware stack automatically.
Route-local middleware still works inside groups.
v1.Post("/posts", requireRole("editor"), createPost)