# Gorilla Mux — Powerful HTTP Router and URL Matcher for Go > Gorilla Mux is a flexible HTTP request router for Go that supports path variables, host-based routing, middleware chaining, and subrouters for modular URL patterns. ## Install Save in your project root: # Gorilla Mux — Powerful HTTP Router and URL Matcher for Go ## Quick Use ```bash go get github.com/gorilla/mux ``` ```go r := mux.NewRouter() r.HandleFunc("/users/{id:[0-9]+}", GetUser).Methods("GET") http.ListenAndServe(":8080", r) ``` ## Introduction Gorilla Mux is one of the most widely adopted HTTP routers for Go. It extends the standard net/http ServeMux with path variables, regex constraints, host matching, and middleware support, while remaining fully compatible with the http.Handler interface. ## What Gorilla Mux Does - Matches routes by path, HTTP method, headers, query parameters, and host - Supports named path variables with optional regex validation - Groups related routes using subrouters for cleaner URL organization - Chains middleware at the router or subrouter level - Generates URLs from named routes for reverse routing ## Architecture Overview Gorilla Mux implements the http.Handler interface. Internally it stores routes in a list and matches requests sequentially against registered criteria (path pattern, method, host, headers, query values). Path variables are extracted via regex groups and stored in the request context. Subrouters inherit parent matchers and allow hierarchical route grouping without a separate framework. ## Self-Hosting & Configuration - Install with `go get github.com/gorilla/mux` - Create a router with `mux.NewRouter()` and register handlers - Use `r.PathPrefix("/api").Subrouter()` to scope middleware to route groups - Enable CORS or logging via standard middleware functions - Pass the router directly to `http.ListenAndServe` or wrap with TLS ## Key Features - Regex-constrained path variables like `{id:[0-9]+}` for type-safe URL matching - Named routes with `r.Name("user")` enable reverse URL generation via `r.URL()` - Subrouters scope middleware and path prefixes to logical groups - Fully compatible with http.Handler, so any standard middleware works - Walks registered routes for documentation or debugging with `r.Walk()` ## Comparison with Similar Tools - **Chi** — Tree-based router with similar API; slightly faster but no regex constraints built-in - **Gin** — Full framework with JSON binding and validation; Gorilla Mux is a pure router - **http.ServeMux** — Standard library default; lacks path variables, methods, and middleware - **Echo** — Framework with its own context; Gorilla Mux uses standard request context - **httprouter** — Faster radix-tree router but less flexible matching and no subrouters ## FAQ **Q: Is Gorilla Mux still maintained?** A: After a brief archival in 2022, the Gorilla toolkit was revived by community maintainers and is actively developed again. **Q: Is Gorilla Mux slower than alternatives like Chi or httprouter?** A: Its linear route matching is slightly slower than tree-based routers for very large route tables, but the difference is negligible for most applications. **Q: Can I use Gorilla Mux with middleware like Alice or negroni?** A: Yes. Because it implements http.Handler, it works with any standard middleware solution. **Q: How do I extract path variables in a handler?** A: Use `mux.Vars(r)` which returns a `map[string]string` of named parameters from the matched route. ## Sources - https://github.com/gorilla/mux - https://gorilla.github.io --- Source: https://tokrepo.com/en/workflows/asset-abf1cbcf Author: AI Open Source