gorilla /mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍

★ 21,829⑂ 1,888👁 297⑃ 351● 446▤ 15
GoBSD-3-ClauseCreated 2012-10-02gorilla.github.io
More from this author
Download: Release v1.8.1Release-1.8.1 ↓1.8.12023-11-05
+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.H

Top issues

Discussions

Latest release

Release v1.8.1 v1.8.1 2023-11-05
What'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:/

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON