explaingit

julienschmidt/httprouter

17,115GoAudience · developerComplexity · 2/5Setup · easy

TLDR

HttpRouter is a fast, lightweight HTTP router for Go that adds named URL path variables and method-based routing to Go web services, using a memory-efficient radix tree so matching stays fast even with many routes.

Mindmap

mindmap
  root((httprouter))
    What it does
      HTTP routing
      Method matching
      Path variables
    How it works
      Radix tree
      Zero allocations
      No ambiguous routes
    Features
      Trailing slash fix
      Static file serving
      Panic handler
      OPTIONS support
    Use Cases
      REST APIs
      Web services
      Microservices
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Build a REST API in Go with named URL parameters like /user/:id without pulling in a heavy web framework.

USE CASE 2

Serve static files from a Go web server alongside API routes using HttpRouter's built-in file serving.

USE CASE 3

Add automatic 405 Method Not Allowed responses and OPTIONS handling to a Go API for REST and CORS compliance.

USE CASE 4

Replace Go's standard library router to get clean URL matching, trailing-slash redirects, and automatic path-mistake fixes.

Tech stack

Go

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

HttpRouter is a lightweight HTTP request router for Go, a programming language commonly used to build web services. A router, also called a multiplexer or mux for short, is the part of a web server that decides which piece of code should handle each incoming request based on the URL and the request method like GET or POST. HttpRouter exists because Go's built-in standard router does not support variables in URL paths and does not distinguish between request methods, which is awkward when building modern web APIs. The library is built around a compact tree structure called a radix tree, which groups routes that share a common prefix. This lets the router quickly find a match even when there are many routes or long URL paths, and matching is designed to allocate almost no extra memory. A request can only match exactly one registered route, so there are no ambiguous overlaps. URL paths can contain named placeholders like :name to capture a single segment, or catch-all placeholders like *filepath to capture everything after a point in the path. Beyond raw matching, the README highlights quality-of-life behavior: it can automatically redirect requests when a trailing slash is wrong, fix common path mistakes like wrong casing or stray slashes, serve static files, and recover from crashes inside a handler through a panic handler. It also has built-in support for OPTIONS requests and 405 Method Not Allowed responses, which makes it a natural fit for hierarchical REST-style APIs. You would reach for HttpRouter when writing a Go web service or API and want predictable, fast routing without pulling in a full web framework. The full README is longer than what was provided.

Copy-paste prompts

Prompt 1
Show me how to set up HttpRouter in a Go project to handle GET /users/:id and POST /users routes, and how to read the :id parameter inside the handler function.
Prompt 2
How do I configure HttpRouter in Go to serve a folder of static files at /static/* alongside my JSON API routes?
Prompt 3
I want to add a panic recovery handler to my Go web server using HttpRouter so a crash in one handler does not take down the whole server. How do I do that?
Prompt 4
How do I add custom 404 Not Found and 405 Method Not Allowed handlers to an HttpRouter-based Go web server?
Prompt 5
Walk me through building a simple CRUD REST API in Go using HttpRouter with routes for creating, reading, updating, and deleting a resource.
Open on GitHub → Explain another repo

← julienschmidt on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.