Cette page est affichée en anglais. Une traduction française est en cours.
SkillsMay 12, 2026·3 min de lecture

httprouter — Lightweight High-Performance HTTP Router for Go

httprouter is a high-performance HTTP request router for Go that uses a radix tree for efficient path matching with zero garbage collection overhead.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
httprouter Overview
Commande CLI universelle
npx tokrepo install a3d92cb5-4db8-11f1-9bc6-00163e2b0d79

Introduction

httprouter is a minimal Go HTTP router focused purely on speed and correctness. It uses a compressed radix tree (trie) for route matching, which means lookups are independent of the number of registered routes. The library is a popular foundation for higher-level frameworks like Gin.

What httprouter Does

  • Matches HTTP routes using a compressed radix tree with zero allocations on the hot path
  • Supports named parameters (:name) and catch-all parameters (*filepath)
  • Implements the standard http.Handler interface for net/http compatibility
  • Detects conflicting routes at registration time to prevent ambiguity
  • Provides automatic OPTIONS and 405 Method Not Allowed responses

Architecture Overview

The router stores each HTTP method's routes in a separate radix tree. When a request arrives, it walks the tree character-by-character, resolving named and wildcard segments along the way. Because the tree is compressed, common prefixes share a single node, keeping memory usage low. Parameters are extracted into a slice that is stack-allocated in most cases, avoiding heap allocation entirely.

Self-Hosting & Configuration

  • Install with go get github.com/julienschmidt/httprouter (Go 1.18+)
  • Register routes with router.GET, router.POST, etc. for each HTTP method
  • Set router.NotFound to a custom handler for 404 responses
  • Set router.PanicHandler to recover from panics inside handlers gracefully
  • Enable trailing-slash redirects with router.RedirectTrailingSlash = true

Key Features

  • Only router in Go with truly zero heap allocation per request
  • Named and catch-all path parameters with no regex overhead
  • Conflict detection at startup prevents silent route shadowing
  • Fully compatible with net/http middleware via http.Handler
  • Battle-tested as the routing engine inside the Gin framework

Comparison with Similar Tools

  • Gorilla Mux — regex-based matching, more flexible but slower lookup
  • Chi — radix tree like httprouter, but adds middleware chaining and context support
  • Gin — full framework built on httprouter with middleware, logging, and JSON helpers
  • net/http DefaultServeMux — simple prefix matching, no parameters or method routing

FAQ

Q: Why choose httprouter over Gin? A: httprouter is just a router. If you want only routing without framework overhead, it is the lighter choice. Gin adds middleware, binding, and rendering on top.

Q: Does httprouter support middleware? A: Not directly. You can wrap handlers manually or use a library like Alice to chain http.Handler middleware.

Q: Can httprouter handle regex routes? A: No. It supports named parameters and catch-alls only. For regex-based routing, consider Gorilla Mux.

Q: Is httprouter still maintained? A: Yes. Development is stable with occasional updates, and its API is considered complete.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires