Documentation Index
Fetch the complete documentation index at: https://mintlify.com/go-chi/chi/llms.txt
Use this file to discover all available pages before exploring further.
Mux is chi’s concrete, high-performance HTTP router. It implements the Router interface and http.Handler, so it can be handed directly to http.ListenAndServe. Internally it uses a radix-trie for O(log n) route lookup and a sync.Pool to reuse routing context objects across requests, keeping per-request allocations low.
Creating a Mux
chi.go
chi.NewRouter() is the canonical constructor. It calls NewMux() and returns a fully initialised *Mux with an empty radix trie and a pre-warmed sync.Pool.
mux.go
main.go
Mux Struct
mux.go
When
true, this Mux is an inline group (created by Group or With) rather than a mounted sub-router. Inline muxes share the parent’s sync.Pool and radix trie but hold their own middleware slice. This flag changes how NotFound and MethodNotAllowed handlers are propagated and how the middleware chain is built when registering an endpoint.Holds recycled
*Context objects. Sub-routers created by With/Group share the root mux’s pool to avoid double-allocation.ServeHTTP — Request Lifecycle
mux.go
ServeHTTP is the single method that makes Mux an http.Handler. Its steps are:
- Guard — if no routes have been registered (
mx.handler == nil) the request is immediately passed to the 404 handler. - Context check — if a chi
*Contextis already present in the request context (i.e. the request is already inside a parent chi router), routing is delegated to the pre-built handler chain without allocating a new context. - Pool get — a
*Contextis retrieved fromsync.Pool, reset to its zero state, and populated with a reference to the currentMuxasrctx.Routes. - Context injection — the context is stored in the request via
context.WithValue(r.Context(), RouteCtxKey, rctx). - Handler execution — the composed handler (middleware chain + trie router) serves the request.
- Pool put — after the handler returns, the
*Contextis returned to the pool for reuse.
mux.go
r.WithContext() causes 2 allocations and context.WithValue() causes 1 allocation per request. The sync.Pool reuse of *Context means the routing context itself does not contribute an allocation on the hot path for sub-routers.Inline vs. Mounted Sub-Routers
Chi has two distinct kinds of sub-router, and theinline field on Mux distinguishes them.
Inline (Group / With) | Mounted (Route / Mount) | |
|---|---|---|
| Path prefix | Inherits parent’s prefix | Adds a new prefix |
| Middleware stack | Copy of parent stack + extras | Independent stack |
| Radix trie | Shared with parent | Own trie |
sync.Pool | Shared with root | Own pool |
NotFound propagation | Propagates to parent mux | Independent |
inline == true, calling NotFound or MethodNotAllowed wraps the provided handler with the inline mux’s own middleware chain before propagating it to the parent — so error responses from an inline group still pass through that group’s middleware.
mux.go
RouteCtxKey
context.go
chi.RouteCtxKey is the context.Context key under which the chi *Context is stored. It uses a private pointer type (*contextKey) as the key to guarantee uniqueness — no accidental collisions with other packages.
Use this key directly only when you need low-level context access. Prefer the chi.RouteContext(ctx) helper.
Additional Mux Methods
Beyond theRouter interface, Mux exposes two concrete handler accessors:
NotFoundHandler
mux.go
http.NotFound when no custom handler has been set.
http.HandlerFunc
The custom not-found handler, or
http.NotFound as the default.MethodNotAllowedHandler
mux.go
methodsAllowed variadic parameter is an internal type (methodTyp) and is not accessible to callers outside the chi package; external code always calls this method with no arguments. The default implementation sets the Allow response header to the list of methods permitted for the matched route and writes status 405. This method is called internally by ServeHTTP when a path matches but the HTTP method does not; it is also useful for testing the configured 405 response directly.
http.HandlerFunc
The custom method-not-allowed handler, or the default 405 responder.
The Context Type
Context is chi’s per-request routing state object. One instance exists for each in-flight request (recycled via sync.Pool).
context.go
A reference back to the
Mux that is currently handling the request. Used internally by Find / Match to delegate into sub-routers.An override for the path being routed. Sub-routers set this to the remaining path after stripping the matched prefix, so each router only sees its own segment.
An override for the HTTP method used during routing. Normally mirrors
r.Method.The accumulated stack of named URL parameters captured across all sub-routers in the chain. Read values via
chi.URLParam(r, key).All routing patterns matched so far across the sub-router stack. The full pattern is assembled by
RoutePattern().Context.URLParam
context.go
URLParams from the end of the stack backwards and returns the most recently captured value for key. Returns an empty string when the key is absent.
The named parameter key to look up.
string
The captured value, or
"" if not present.Context.RoutePattern
context.go
RoutePatterns into a single canonical pattern string and trims any trailing slashes or wildcard artifacts. The returned value is the complete matched pattern from the root mux down through all sub-routers.
middleware.go
RouteParams
context.go
Parameter names in capture order.
Corresponding captured values, aligned index-for-index with
Keys.RouteParams.Add
context.go