# 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. ## Install Save in your project root: # httprouter — Lightweight High-Performance HTTP Router for Go ## Quick Use ```bash go get github.com/julienschmidt/httprouter ``` ```go package main import ( "fmt" "net/http" "github.com/julienschmidt/httprouter" ) func main() { router := httprouter.New() router.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, "Hello, %s!", ps.ByName("name")) }) http.ListenAndServe(":8080", router) } ``` ## 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 - https://github.com/julienschmidt/httprouter - https://pkg.go.dev/github.com/julienschmidt/httprouter --- Source: https://tokrepo.com/en/workflows/asset-a3d92cb5 Author: AI Open Source