API Reference

Gorilla Mux

URL router and dispatcher for Go services with named routes, path variables, and middleware chaining.

Official Documentation ↗
CompatibilityThis documentation covers version v1.8. Check the official docs for the latest API surface.

Installation

gogo get github.com/gorilla/mux

Methods

URL router and dispatcher for Go services with named routes, path variables, and middleware chaining.

Overview

Gorilla Mux routes HTTP requests with expressive matching: paths with named variables and inline regex constraints, method matching, host and scheme conditions, and middleware chains applied per router or subrouter.

Subrouters group related endpoints under shared prefixes and middleware, while named routes enable reverse URL generation. Handlers remain standard net/http signatures, so the router composes with the entire Go ecosystem.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • HandleFunc / Methods — Registers a handler with a path pattern and constrains it to HTTP methods.
  • Path variables / Vars — Extracts named path segments with inline regex constraints into a map.
  • Subrouter — Mounts a scoped router under a prefix so groups share middleware and prefixes.
  • Use / middleware — Wraps all matched routes with middleware executed in registration order.

Usage Notes

  • Constrain variables in the pattern, {id:[0-9]+}, instead of validating after extraction.
  • Prefer subrouters over repeated prefixes so middleware boundaries match API areas.
  • Use r.NotFoundHandler to return your JSON error envelope instead of the default text 404.

Common Pitfalls

  • Registering the same path with different methods requires .Methods on each route; otherwise registration panics on duplicate.
  • Vars are only available inside the matched handler; reading them in middleware returns an empty map.
  • PathPrefix matches raw subtrees, pair it with Subrouter to keep route definitions precise.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/gorilla/mux@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:00.862Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

How does it compare with the standard library ServeMux?
Since Go 1.22, net/http mux supports methods and wildcards natively, which covers simple cases. Gorilla Mux adds regex segment constraints, named routes with URL building, subrouters, and route matching on host, scheme, headers, and query values.
How do I build URLs from routes?
Name a route with .Name("order") and call r.Get("order").URL("id", "42") to reverse it into a *url.URL. This keeps links refactor-proof instead of format strings scattered everywhere.
What is the middleware execution order?
Middleware registered with Use runs outermost-first in registration order, wrapping the final handler. A subrouter composes its middleware after the parent router matching has begun, so parent middleware still encloses it.