+0
Daily
-4
Weekly
-6
Monthly
21,829
stars
1,888
forks
297
watchers
351
prs
446
issues
15
releases
README
gorilla/mux
!testing
!Gorilla Logo
Package gorilla/mux implements a request router and dispatcher for matching incoming requests to
their respective handler.
The name mux stands for "HTTP request multiplexer". Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
It implements the http.Handler interface so it is compatible with the standard http.ServeMux.
Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
URL hosts, paths and query values can have variables with an optional regular expression.
Registered URLs can be built, or "reversed", which helps maintaining references to resources.
Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
---
Install
Examples
Matching Routes
Static Files
Serving Single Page Applications (e.g. React, Vue, Ember.js, etc.)
Registered URLs
Walking Routes
Graceful Shutdown
Middleware
Handling CORS Requests
Testing Handlers
Full Example
---
Install
With a correctly configured Go toolchain:
sh
go get -u github.com/gorilla/mux
Examples
Let's start registering a couple of URL paths and handlers:
go
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/articles", ArticlesHandler)
http.Handle("/", r)
}
Here we register three routes mapping URL paths to handlers. This is equivalent to how http.HandleFunc() works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (http.ResponseWriter, http.Request) as parameters.
Paths can have variables. They are defined using the format {name} or {name:pattern}. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:
go
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
The names are used to create a map of route variables which can be retrieved calling mux.Vars():
go
func ArticlesCategoryHandler(w http.ResponseWriter, r http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Category: %v\n", vars["category"])
}
And this is all you need to know about the basic usage. More advanced options are explained below.
Matching Routes
Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:
go
r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.HTop issues
#703[bug] Headers are not accessible in Middleware when Request sent from Browser10 comments · 2022-11-21#783[Queston] is gorilla/mux Still Maintained?9 comments · 2025-05-12#781[BUG] Semicolon unduly acts as separator for query parameters (thereby creating a parser differential)9 comments · 2025-03-17#669[question] How to get the Url without Path Params8 comments · 2022-02-03#694[bug] Using route.Methods(...) more than once7 comments · 2022-09-12#749[BUG] runtime error in (*routeRegexp).Match6 comments · 2024-01-10#754[FEATURE] Support multi-value query string parameters4 comments · 2024-03-29#686why return a router for the methods strictslash, skipClean and use Encoded Path?4 comments · 2022-07-18
Discussions
#790Changenotes — free AI changelog generator for mux0 comments · 2026-03-17#771Welcome to mux Discussions!0 comments · 2024-09-29#772[BUG] no required module provides package2 comments · 2024-09-27
Latest release
Release v1.8.1 v1.8.1 2023-11-05What's Changed build: CircleCI 2.1 + build matrix by @elithrar in https://github.com/gorilla/mux/pull/595 Include "404" and "405" in the docs by @Jille in https://github.com/gorilla/mux/pull/602 docs: update README w.r.t new maintainer ask by @elithrar in https://github.com/gorilla/mux/pull/660 regexp: use iota instead of hardcoded values for regexType by @michaelgrigoryan25 in https://github.com/gorilla/mux/pull/679 Fix authenticationMiddleware initialization in the README.md file by @amustaque97 in https://github.com/gorilla/mux/pull/693 Update README.md by @coreydaley in https:/