explaingit

gorilla/mux

21,848GoAudience · developerComplexity · 2/5StaleLicenseSetup · easy

TLDR

A powerful URL router for Go web servers that matches requests by path, HTTP method, domain, headers, and query parameters, more flexible than Go's built-in router.

Mindmap

mindmap
  root((gorilla/mux))
    What it does
      URL pattern matching
      Variable extraction
      Request filtering
    Matching options
      HTTP methods
      Domains and subdomains
      Headers and schemes
      Query parameters
    Key features
      Subrouting for grouping
      Middleware support
      Static file serving
    Use cases
      Web applications
      REST APIs
      Single-page apps

Things people build with this

USE CASE 1

Build a REST API that routes requests to different handlers based on URL path, HTTP method, and domain.

USE CASE 2

Create a multi-tenant web application where different subdomains route to different subrouters.

USE CASE 3

Add middleware to log requests, check authentication, or modify responses across multiple routes.

USE CASE 4

Serve a single-page application with dynamic URL patterns that extract IDs or categories from the path.

Tech stack

GoHTTP

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose, including commercial use, as long as you keep the copyright notice.

In plain English

Gorilla mux is a routing library for Go web servers. When you build a web server, you need a way to decide which function handles each incoming URL, for example, requests to /products go to one function and requests to /articles go to another. Go's standard library includes a basic router called http.ServeMux, and gorilla mux is a more powerful alternative that plugs into the same interface; the name mux is short for HTTP request multiplexer. The library lets you define URL patterns that include variable placeholders. A route like /articles/{category}/{id} captures the category and id from the URL and makes them available inside the handler. Routes can also be restricted by HTTP method, by domain or subdomain, by URL scheme, by header values, by query parameters, or by a custom matching function you write yourself. Multiple conditions can be combined on a single route, and routes are tested in the order they were added so the first match wins. A feature called subrouting lets you group routes that share common conditions, such as all routes for a particular subdomain or under a path prefix. The parent condition is tested first, and inner routes are only checked when it matches, convenient for organization and slightly faster. Registered URLs can also be reversed, meaning you ask the router to generate a URL for a named route, which keeps links inside the application correct as routes change. Because gorilla mux satisfies Go's standard http.Handler interface, it plugs into existing Go HTTP code without special setup. It supports middleware, serving static files, and serving single-page applications alongside an API. The full README is longer than what was provided.

Copy-paste prompts

Prompt 1
Show me how to set up a gorilla/mux router that matches GET requests to /articles/{id} and extracts the id parameter.
Prompt 2
How do I use gorilla/mux subrouters to handle requests for different subdomains (api.example.com vs www.example.com)?
Prompt 3
Write a gorilla/mux route that only matches HTTPS requests with a specific header value, and explain how to combine multiple conditions.
Prompt 4
How do I add middleware to a gorilla/mux router to log all incoming requests before they reach the handler?
Prompt 5
Show me how to serve static files and handle 404 errors with gorilla/mux.
Open on GitHub → Explain another repo

Generated 2026-05-21 · Model: sonnet-4-6 · Verify against the repo before relying on details.