Routing
Learn Kern routing for Go web services, including HTTP methods, path parameters, wildcards, route groups, static files, and stdlib-native route matching.
HTTP Methods
Kern provides helper methods for all standard HTTP verbs. This makes route definition declarative and easy to read. Since Kern builds on top of net/http, these are fully compatible with standard handlers.
app := kern.Default()
// Basic routes
app.GET("/users", listUsers)
app.POST("/users", createUser)
app.PUT("/users/{id}", updateUser)
app.PATCH("/users/{id}", patchUser)
app.DELETE("/users/{id}", deleteUser)
// Other methods
app.HEAD("/status", checkStatus)
app.OPTIONS("/api", showOptions)Path Parameters
Kern supports two syntaxes for path parameters:
| Syntax | Example | Origin |
|---|---|---|
{name} | /users/{id} | Go 1.22+ stdlib (recommended) |
:name | /users/:id | Gin / Chi compatibility |
Both are interchangeable. Use whichever you prefer.
Capturing Values
To retrieve a parameter value, use c.Param("name").
// Route: /users/:id
app.GET("/users/:id", func(c *kern.Context) {
id := c.Param("id")
c.Text(200, "User ID: %s", id)
})
// Equivalent:
app.GET("/users/{id}", func(c *kern.Context) {
id := c.Param("id")
c.Text(200, "User ID: %s", id)
})Multiple Parameters
You can define multiple parameters in a single route.
// Route: /posts/:category/:id
app.GET("/posts/:category/:id", func(c *kern.Context) {
category := c.Param("category")
id := c.Param("id")
c.JSON(200, map[string]string{
"category": category,
"id": id,
})
})Wildcards
Use {name...} to match the remaining path segments. This is useful for file servers or catch-all routes.
// Matches /files/images/logo.png
app.GET("/files/{path...}", func(c *kern.Context) {
path := c.Param("path")
c.Text(200, "Requested file: %s", path)
})Route Groups
Grouping routes allows you to organize your application structure and share common middleware (like authentication or logging) across a set of routes.
// Create a group for API v1
v1 := app.Group("/api/v1")
// Apply middleware specific to this group
v1.Use(authMiddleware)
// Define routes under /api/v1
{
v1.GET("/users", listUsers) // GET /api/v1/users
v1.POST("/users", createUser) // POST /api/v1/users
}
// Create a nested group for admin
admin := v1.Group("/admin")
{
admin.DELETE("/users/{id}", deleteUser) // DELETE /api/v1/admin/users/{id}
}Static Files
Serve static files from a directory using app.Static. This automatically sets up a file server for the specified directory.
// Serve files from the "./public" directory at the "/static" path
// e.g. /static/css/style.css -> ./public/css/style.css
app.Static("/static/", "./public")Ensure the path prefix ends with a slash / (e.g., /static/) for proper routing behavior.
Routing strategy tips
- Use constrained or clearly scoped routes for predictable API behavior.
- Use route groups to keep versioned APIs and admin surfaces organized.
- Use wildcards for file serving and catch-all behavior only when needed.
See Also
- Nested Routes Example - Demonstrates deeply nested route groups
- REST API Example - Full CRUD with route groups
- Context API - Read params, queries, and body data in handlers
- Architecture Flow - Understand how routing fits into middleware and response flow
FAQ
Does Kern use Go's built-in router?
Yes. Kern builds on Go 1.22+ net/http routing semantics instead of shipping a separate routing engine.
Can I organize large APIs with groups?
Yes. Route groups are the preferred way to structure versioned APIs, protected admin areas, and shared middleware layers.
Migration Guide
Upgrade existing Go services to newer Kern routing, middleware, sessions, request guards, and performance-oriented helper APIs with minimal disruption.
Context API
Learn the Kern Context API for Go request parsing, response writing, file uploads, downloads, headers, cookies, and efficient pooled request handling.