API Reference

httprouter

High-performance Go HTTP router using a radix tree to match routes with named parameters in constant-time paths.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

gogo get github.com/julienschmidt/httprouter

Methods

Overview

httprouter is a high-performance Go HTTP router using a radix tree to match routes with named parameters efficiently. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • httprouter.New — Creates a router implementing http.Handler with zero allocations on matching.
  • router.GET — Registers a handler for GET on the given path, which may contain named parameters.
  • httprouter.Params — Reads a matched named parameter from the request params slice.

Example

router := httprouter.New()
_ = http.ListenAndServe(":8080", router)

Notes

It rejects ambiguous route patterns at registration time, which avoids unpredictable matching but requires careful path design.

Verification

Register a parameterized route and confirm a request returns the parameter value parsed from the URL.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/julienschmidt/httprouter@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:17.832Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

How is httprouter different from the standard mux?
It uses a radix tree and supports named parameters with near zero overhead, but it does not support regular expression patterns or method-agnostic routes.
Why does httprouter panic on conflicting routes?
It forbids ambiguous patterns such as static and named segments at the same position, which prevents unpredictable matching.
How do I serve static files?
Use router.ServeFiles with the path prefix and directory, which streams files through the standard handler.