Documentation

Docs / Reference

Claim contract: Go HTTP routers

The claim contract of the Go HTTP router extractor and linker (ADR 0076) as recorded when it was designed (task T023, internal record). docs/extract/routes.md (Go claim contract) and the code table in internal/extract/routes must equal these rows (TestGoRoutesContract).

Claim contract (draft)

Row kinds: K recognition and scope; O router objects; R registrations; P patterns; C composition; H handlers and attributes; V assumptions (above, no branch); D the single default branch. The implementation contract test keeps this table, the Go table in internal/extract/routes (file gohttp_contract.go) and docs/extract/routes.md's new Go section 1:1, checks one tagged branch per claim row, and checks that the fixture reaches every row, every listed form of a multi-form row and every D form. 32 rows: 31 rows plus D (GR-V counted as one, as GO-V in T018g5; GR-K4 and the escaped_router refusal of GR-C4 were added as reported contract changes; GR-C6 and assumption GR-V5, on the decision on GR3's open points; GR-O7, the srv.Handler = h form of GR-C6 and the plain-handler exception of GR-C3, on developer decision GO1; GR-G1 and GR-C4's method-chain refusal, on developer decision GO2).

id modelled form facts claimed refusal (to D, counted form) / diagnostic
GR-K1 a Go file of a scanned module (GS-M1) importing net/http, github.com/go-chi/chi/v5 or github.com/gin-gonic/gin, by plain or aliased import (resolved as GS-I4 external imports; a scanned module with one of those paths shadows it) recognition: the file's router calls can be read dot import of a router package (dot_import); github.com/go-chi/chi before v5 (unsupported_module); for net/http registrations, GR-V2 not met (legacy_mux_patterns)
GR-K2 constructors http.NewServeMux(), chi.NewRouter(), chi.NewMux(), gin.New(), gin.Default(); the DefaultServeMux through package-level http.Handle, http.HandleFunc and http.DefaultServeMux a router object (framework, creation site) -
GR-K3 ordered local bindings in one function body: :=, =, var x = e, var x T = e (an interface type such as http.Handler included), shadowing in nested blocks, as Axum's ordered let bindings the object a name holds at each use a binding that holds an unproven value on some path reaching the use (if r == nil { r = chi.NewRouter() } after r := options.BaseRouter), a struct field, package-level variable, map or slice element, type assertion or channel receive as receiver (receiver_unproven)
GR-K4 zero-value ServeMux objects: var m http.ServeMux, m := http.ServeMux{} (or var m = http.ServeMux{}), &http.ServeMux{}, new(http.ServeMux) (Go 1.27.1 net/http/server.go: NewServeMux returns &ServeMux{} and DefaultServeMux is a zero ServeMux, so the zero value is a ready mux) a router object (net/http, creation site); a value variable is read as a receiver, through & and as m.ServeHTTP a value variable read as a plain value (a copy of the mux) holds no object (receiver_unproven when it is a receiver)
GR-O1 chi r.Route(p, f) and r.Group(f), f a function literal or a module function (identifier or pkg.F) whose first parameter is chi.Router a child object: Route with prefix p, Group with the same path; the callback body unconditional relative to the call (GR-V3); Route and Group also return that child any other callback (callback_unread)
GR-O2 chi r.With(mw...) an inline child with the same path; mw are the middleware of registrations made directly on it -
GR-O3 gin g.Group(p, mw...); g.Use(mw...) used as a value Group: a child with prefix p (GR-C2); Use: the same object -
GR-O4 a router object passed as an argument at an explicit call of a module function or method resolved by the Go linker's rules, to a parameter declared *http.ServeMux, chi.Router, *chi.Mux, *gin.Engine, *gin.RouterGroup, gin.IRouter or gin.IRoutes (ADR 0019's helper rules: each call its own context, call-site conditions OR body conditions) the parameter holds that object in that call's context a parameter that is reassigned (param_rebound); a parameter no resolved call binds keeps its registrations without a path (route_unmounted, as gotify's plugin RegisterWebhook)
GR-O5 a call of a module function or method whose every return returns a proven object of one framework (recursively, cycle-guarded) the call's value is that object (each object once, not copied) a method called on a call result other than GR-O9's (factory_method_chain); a function with another return
GR-O6 x.ServeHTTP (method value) of a proven object; a conversion http.Handler(x) the object itself -
GR-O7 path-preserving wrappers: a call f(x), pkg.F(x) or v.M(x) (v typed as GR-H2 types a receiver) of a module function or method with one http.Handler parameter and one http.Handler result whose syntax passes the wrapper proof (docs/lang/go-routes.md, "Wrappers and request helpers": every return is the parameter or http.HandlerFunc(func(w, r) {...}); the parameter is used only in h.ServeHTTP(w, r) or h.ServeHTTP(w, r.WithContext(c)) statements of such a literal; every use of r that may run before or during such a call reads the request, rebinds r = r.WithContext(c) or passes r to a module function or method that only reads it, recursively), x its http.Handler argument the call holds x's objects, as http.Handler(x) does (GR-O6): compositions (GR-C1, GR-C3) and serving positions (GR-C6) read through it, without assumption GR-V5 (Go 1.27.1: ServeMux dispatches on the request's RequestURI, Proto, Method, Host and URL, which the proof leaves unchanged; r.WithContext shares the URL) a wrapper that serves r.WithContext(c) around a chi router (chi keeps its routing state in the context); any other wrapper: at a composition position GR-C4's wrapped_router, at a serving position GR-C6's assumption GR-V5; a wrapper with a branch that ends or serves chosen by a condition reading r.URL or r.RequestURI (it answers some paths itself) or calling a request helper that reads the URL or the method, directly or through its passes, or that no rule resolves: not path-preserving; a wrapper that answers a method itself in if r.Method == M { ...; return } (M a string literal or http.MethodX, no else) or a switch on r.Method with ending clauses: routes of M and ANY routes get no path through it, any other deciding read of r.Method refuses the proof
GR-O8 forwarding closures: a function literal func(w, r *http.Request) that is the handler argument of a GR-C7 registration and passes the forwarding proof (docs/lang/go-routes.md, "Forwarding closures": its only serving calls are X.ServeHTTP(w, r) statements with the request unchanged and outside every branch chosen by r.URL; every use of r before or during them reads it or passes it to a request helper; r.URL is read only as r.URL.Path switched on with string-literal cases or compared == with a string literal in an if condition), X a local variable written exactly once (its declaration) or a parameter never assigned, whose value holds proven chi router objects the literal holds X's objects, as http.Handler(X) does (GR-O6); the string literals r.URL.Path is switched on or compared with are shadows: a composed path one of them may match is refused (GR-C7) any other literal, a closure forwarding to a ServeMux, X written more than once, a pass GR-O7's helpers do not prove: GR-C4's wrapped_router as before; a deciding condition that calls a request helper reading the URL or the method, or one no rule resolves: no proof; a method answered in if r.Method == M { ...; return }: routes of M and ANY routes get no path through the closure
GR-O9 a method called on a call result: f(args).M() or pkg.F(args).M() with f a module function that declares exactly one result of a module type T or *T (GR-H2's typing of a call result) and M a captured method of T (handlers.New(h).Handler()) the call's value is what GR-O5 reads from M's returns (objects built inside M, or a generated server's router, GR-G1) M returning anything else (a field of the receiver, a parameter, a value no row proves) or not resolved (factory_method_chain)
GR-R1 net/http m.Handle(p, h) / m.HandleFunc(p, f) on a ServeMux object (GR-K2), p constant (GR-P1) and accepted by Go 1.27.1's pattern parser, h not a router object (GR-C3 reads those) one route: method from the pattern (GET /x -> GET, which also answers HEAD; no method -> ANY); host attribute for a host pattern; pattern the literal as written a pattern the parser rejects or that conflicts with an earlier pattern on the same mux (invalid_pattern, warning route_invalid_pattern)
GR-R2 chi Get/Post/Put/Patch/Delete/Head/Options/Connect/Trace(p, h); Handle(p, h), HandleFunc(p, f) (ANY, or the method of a "METHOD /p" pattern, PS-GR-4); Method(m, p, h), MethodFunc(m, p, f) with m a string constant or http.Method* one route per registration with that method a non-constant method (dynamic_method, warning route_dynamic_methods); a method chi rejects or a pattern not starting with / (invalid_pattern)
GR-R3 gin GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS(p, h...); Any(p, h...) (ANY); Handle(m, p, h...) with constant m; Match(ms, p, h...) with ms a slice literal of string constants or http.Method* one route per distinct method (Any: one ANY route) a non-constant method or methods list (dynamic_method)
GR-R4 gin Static(p, dir), StaticFS(p, fs), StaticFile(p, file), StaticFileFS(p, file, fs) GET and HEAD routes; path p/{*filepath} for Static/StaticFS, p for the file forms (PS-GR-2); handler null, handler_expression the call's method name -
GR-P1 a pattern or prefix that is a string literal, a constant (local const, a package-level const of the module's packages, imported), or a + of those (Go constant evaluation) the pattern text; path resolution: literal for literals, constant otherwise any other expression (dynamic_pattern: warning route_dynamic_pattern, no route; for a prefix route_prefix_unresolved, no path)
GR-P2 template normalization of a composed path (shared slash rules of routes.md) gin :x -> {x}, *x -> {*x}; chi {x} kept, {x:re} -> {x} (as FastAPI's convertors), * -> {*}; net/http {x} kept, {x...} -> {*x}, a trailing {$} -> exact path with trailing_slash: true, a subtree pattern /a/ -> /a/{*}; a trailing / elsewhere -> trailing_slash: true (ADR 0019's flag, now for all three frameworks) a chi regexp that can match / (regexp_param: the route keeps its node, no path)
GR-C1 chi r.Mount(p, x) with x a proven chi router object (GR-K2, GR-O5); Route (GR-O1) composes the same way the child's paths are TrimSuffix(prefix, "/") + child path (Route("/nested/") + / = /api/nested/, oracle); a child's / route gets the prefix's template with trailing_slash (it answers prefix/; Mount("/api", x) also answers the bare /api, so the route also gets the bare prefix's entry without trailing_slash (Mount's stub registration, TOP-1 of 2026-11-18; checked against the parent tree as any composed path), while a prefix written with a trailing slash, Mount("/api/", x) or Route("/c/", ...), does not: chi v5 Mount) x not a proven chi router, a router of another framework included (mount_unproven_handler, route_mount_unresolved); a prefix chi rejects (not starting with /: invalid_pattern) or with a regexp that can match / (regexp_param): no path through it; a registration of the same method (or ANY) in a parent tree (the router mounted on, its Group and With children and bound parameters) that matches every request of the composed path relative to that tree, segment by segment (a static segment equal, a parameter, a regexp parameter matching a static segment; step GR8): no path through the mount for that route (chi tries static and parameter nodes before the mount's catch-all)
GR-C2 gin group prefixes joinPaths (gin utils.go): path.Join(base, p), keeping a trailing slash of p; the engine's base is / (PS-GR-1); an empty pattern is the group's path -
GR-C3 net/http m.Handle(outer, http.StripPrefix(prefix, x)) and m.Handle(outer, x) / m.HandleFunc(outer, x.ServeHTTP) with x a proven ServeMux (a chi router at /: GR-C7) the child's paths are prefix + child path ("" without StripPrefix), claimed through this chain only when outer (method, host and path) matches every request of the child route (PS-GR-3) a prefix or outer pattern that is not constant, or a prefix holding { or } (route_prefix_unresolved); an outer pattern that conflicts with an earlier pattern on its mux (invalid_pattern); a handler that may also hold an unproven value (route_mount_unresolved; a binding of http.HandlerFunc(func(...){...}) whose literal uses no router object is no unproven value: the chain holds through the router's bindings, conditional per GR-C5): no path through it; a sibling pattern on the hop's parent mux more specific than outer that matches every request of the composed pattern (the path is never dispatched to the route; a partial overlap stays the matcher's precedence), the bare path of a sibling subtree pattern that does when outer ends in a wildcard (a redirect), or a parent mux with a Handle call of unknown pattern or that escapes, a sibling in the other branch of an if than the composition excepted: no path through the hop for that route
GR-C4 roots every proven constructor object no resolved composition consumes, and every use of which a row reads (step GR8: a registration's or child's receiver, a router's own methods, a registration's or composition's handler, a serving position, a local binding, a GR-O4 argument, a GR-O7 wrapper's handler, net/http's StripPrefix, TimeoutHandler, MaxBytesHandler, Handler and HandlerFunc around such a use, the http.Handler argument of an http.Handler-returning call bound locally or served, a router passed to an http.Handler parameter its module callee only serves at its own paths (an http.Server's Handler, a serving call, srv.Handler = h), the handler of httptest.NewServer, NewTLSServer and NewUnstartedServer, a return GR-O5 or GR-O9 reads, an http.Server literal's Handler, a GR-G1 entry call), is a root (ADR 0016's rule), including dead code and test files; paths are composed from every root through resolved compositions, per-chain cycle guards, all distinct reachable paths kept an object some use of which no row reads (passed to a parameter GR-O4 does not bind, an http.Handler, interface, any or reassigned parameter included, or to a call no rule resolves whose value is not bound locally; stored in a field, element, dereference, package-level or enclosing variable; sent on a channel; returned where GR-O5 and GR-O9 do not read the return, a multi-result constructor included; used through a method value or a method other than the router's own, gin's Handler() included; a handler-shaped closure using it in such a position): escaped_router, route_mount_unresolved naming the use, no root, and a parameter's escape is its bound objects' (the payload's escape records, row RU-escape, cover the syntax it does not keep); an object that reaches a composition position (the handler argument of Handle, HandleFunc, Mount, StripPrefix, directly or through bindings and returns) only inside another call or a closure other than a GR-O7 wrapper or GR-O8 forwarding closure (wrapped_router, route_mount_unresolved naming the call; open point O1); an object stored into a composite literal (a struct, slice or map element; the Handler field of an http.Server literal excepted, which serves it as a root) (escaped_router, route_mount_unresolved naming the element); a router served by an http.StripPrefix no GR-C3 composition or GR-C6 serving position reads (wrapped_router); a router returned by a method called on a call result whose receiver type the call's declared result states, where GR-O9 does not prove the value (step GR6: the composition stays refused, the router is wrapped_router and no root); a router used directly as a registration's handler is no root either (its routes: route_unmounted)
GR-C5 conditional and scope conditional: true for registrations, compositions or bindings inside if/else, switch/select cases, for/range bodies, closures other than GR-O1 callbacks, or helper bodies whose call is conditional (ADR 0019 OR rule), and for a composition whose handler may instead hold such a plain http.HandlerFunc literal; path-entry scope: test only when every reaching chain is test-scope (routes.md) -
GR-C6 serving positions: the handler argument of net/http's ListenAndServe, ListenAndServeTLS, Serve and ServeTLS, the Handler field of an http.Server literal and the value of a srv.Handler = h assignment with srv stated http.Server or *http.Server as GR-H2 types a receiver, followed through bindings, &, x.ServeHTTP, the http.Handler and http.HandlerFunc conversions, http.TimeoutHandler and http.MaxBytesHandler (path-preserving in Go 1.27.1's source), the returns of a module function, http.StripPrefix(p, x), path-preserving wrappers (GR-O7) and any other wrapper call w(..., x, ...) a root served through http.StripPrefix(p, x) has the paths p + its path (Go 1.27.1 StripPrefix removes p before the inner router sees the path; nested prefixes in order); a root served through a wrapper call keeps its paths with assumption GR-V5 on every path entry (assumptions: ["GR-V5"], served_through naming the wrappers), dropped when another chain reaches the same path without a wrapper; a root with serving positions is served only through them (a serving position in a test-scope file, or reached through a binding in one, does not serve a production root) a prefix that is not constant, does not start with /, ends with / or holds {, }, * or : (route_prefix_unresolved: no path through it, and a root served only that way is no root); a router used inside a function literal at a serving position (wrapped_router, route_mount_unresolved)
GR-C7 a ServeMux -> chi hop: net/http m.Handle("/", x) or m.HandleFunc("/", x.ServeHTTP) with x a proven chi router, or m.HandleFunc("/", f) with f a forwarding closure of one (GR-O8); the outer pattern exactly / (no method, no host), no StripPrefix, m a root ServeMux (no net/http hop before it) the chi router's paths are unchanged (Go 1.27.1 ServeMux.ServeHTTP passes the request on, setting only r.Pattern and the wildcard matches; chi routes on r.URL.RawPath/r.URL.Path), claimed only when no other pattern registered on m (or on a parameter bound to it), no /tree -> /tree/ redirect of a sibling subtree pattern and no shadow of the closure may match any request of the path (the chi path read as a ServeMux pattern: parameters as wildcards, a final * as {x...}, a final / as {$}) and the path is clean; the route states assumption GR-V2 (the mux's Go 1.22 patterns) an outer pattern other than /, a router of another framework, a StripPrefix (not composed: the router stays a handler, route_unmounted); a mux with a Handle call of unknown pattern, a path a sibling, redirect or shadow may take, a parameter inside a segment, a path ServeMux cleans: no path through the hop (route_mount_unresolved naming the route); a mux that escapes (GR-C4: a sibling may be registered where no rule sees it) or the DefaultServeMux (packages such as net/http/pprof and expvar register on it in init; step GR8): the gate is opaque, no path through the hop
GR-H1 the handler argument (net/http and chi: the handler position; gin: the last argument) is a module function: identifier or pkg.F handler = the function's identity; exposes_route edge handler -> route (fact, confidence 1, registration evidence) -
GR-H2 a method value x.M whose receiver type is stated by the syntax: a receiver or parameter declared T/*T, a var with a type, a composite literal T{...}/&T{...}, or a call of a module function with one declared result type, T a module named type with method M (GS-D4) as GR-H1, to the method T.M -
GR-H3 http.HandlerFunc(f) with f a GR-H1 or GR-H2 form as f -
GR-H4 a function literal handler_inline: true, no edge -
GR-H5 any other handler expression (a call result such as mw.RequireAdminAuth(h.Index), an interface method value, a local value, a field) handler: null, handler_expression (whitespace-free), no edge -
GR-H6 registration attributes framework (net/http, chi, gin); method; pattern (literal as written, method and host included for net/http); qualified name METHOD <pattern without its method prefix>, @2, @3 for repeats in a file; middleware: gin arguments before the handler, chi With(...) arguments of the receiver chain (registration-site middleware only: Use and group middleware are not listed); host (net/http); assumptions (GR-V1, GR-V2 when they apply) -
GR-G1 oapi-codegen generated servers (developer decision GO2): a file whose payload carries the generated-server record (docs/lang/go-routes.md, row RO-server: the header // Code generated by github.com/oapi-codegen/oapi-codegen/v2 version vX.Y.Z DO NOT EDIT. of v2.8.0, v2.8.1 or v2.8.2 and the server declarations of the chi server, gin server or net/http server output equal to those versions' templates token for token, every registered wrapper.N calling siw.Handler.N once); its instances are the resolved calls of its entry functions outside the template's own functions: Handler(si), HandlerFromMux(si, r) (and HandlerFromMuxWithBaseURL(si, r, u)), HandlerWithOptions(si, o), gin RegisterHandlers(router, si) and RegisterHandlersWithOptions(router, si, o) the router local of HandlerWithOptions holds every instance's router: a new router (the template's constructor) for Handler and for an options literal without BaseRouter, else the proven routers of the BaseRouter name; the entry call's value is its instance's router; gin's router parameter binds as GR-O4; each registration's pattern is the server's BaseURL ("" for Handler, HandlerFromMux and RegisterHandlers, else the constant u or the options literal's BaseURL element, GR-P1, "" when absent) followed by the template's path (net/http: after the method), when every instance gives the same BaseURL; the handler wrapper.N is method N of the module struct type every instance states for si (typed as GR-H2 types a receiver, or a composite literal or call result; implementing method), with the exposes_route edge an instance whose BaseURL is not constant or differs from another instance's (route_prefix_unresolved at the instance: every route of the server keeps its node, no path); a BaseRouter that is not a name holding proven routers of the server's framework, or options that are no composite literal (receiver_unproven); no visible instance: no path (route_unmounted); a si type that is not one module struct type: handler: null, handler_expression ServerInterface.N; a file failing the header or shape check is read by the other rows (its registrations are receiver_unproven)
GR-V assumptions GR-V1-GR-V5 (Environment) stated on the routes -
D anything else nothing; counted per file in one info diagnostic go_routes_unread with (form=n, ...), plus the shared route diagnostic where one applies -

D forms (counted): dot_import, unsupported_module, legacy_mux_patterns, receiver_unproven, callback_unread, param_rebound, factory_method_chain, invalid_pattern, dynamic_method, dynamic_pattern, regexp_param, mount_unproven_handler, wrapped_router, escaped_router. Not modelled (no claim, not counted): Use, NotFound, MethodNotAllowed, NoRoute, NoMethod, gin LoadHTML*, gin Run, an http.Server's Handler set through a field or element (s.srv.Handler = h), and every method on an object that is not a router (HTTP clients).

Not claimed (stated in docs, no fact): route precedence between overlapping patterns (the client matcher keeps its own specificity scoring), group-level middleware, trailing-slash redirects (gin's RedirectTrailingSlash, net/http's subtree redirect), and chi's NotFound handlers.

Report a problem with this pageSource: docs/contract/claims/go-http-routers.md