Skip to main content

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​

MethodPurpose
UseAdd group-local middleware
Group, RouteCreate nested route trees
Get, Post, Put, Patch, Delete, Head, Options, Connect, TraceRegister routes under the group prefix
Add, Match, All, AnyRegister more general route sets
HandleRegister a named route with RouteSpec
RouteNotFoundRegister a prefix-scoped not-found route
Mount, Static, StaticFS, File, FileFSMount 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)