# Chi — Lightweight Composable HTTP Router for Go > Chi is a lightweight, idiomatic HTTP router for Go built on net/http, offering middleware composition, subrouting, and context-based request handling. ## Install Save as a script file and run: # Chi — Lightweight Composable HTTP Router for Go ## Quick Use ```bash go get github.com/go-chi/chi/v5 # In main.go: # r := chi.NewRouter() # r.Use(middleware.Logger) # r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) }) # http.ListenAndServe(":3000", r) ``` ## Introduction Chi is a composable router for building Go HTTP services. It is built entirely on the standard library's net/http package, so handlers and middleware are fully compatible with the Go ecosystem without any framework lock-in. ## What Chi Does - Routes HTTP requests using a radix tree with URL parameter and wildcard support - Composes middleware in a stack-based chain that wraps standard http.Handler - Groups routes into sub-routers that share middleware and path prefixes - Passes request-scoped values through Go's context.Context on every request - Ships with a rich set of optional middleware for logging, compression, CORS, and more ## Architecture Overview Chi's router is a compressed radix tree (trie) that matches incoming request paths segment by segment. Each node in the tree can hold middleware chains and handler references. When a request arrives, the router walks the trie, collects URL parameters into the request context, and executes the middleware chain in LIFO order before calling the final handler. Because Chi uses net/http types throughout, any third-party middleware written for the standard library works without adapters. ## Self-Hosting & Configuration - Add to your project with `go get github.com/go-chi/chi/v5` - Import middleware from `github.com/go-chi/chi/v5/middleware` for common patterns - Mount sub-routers with `r.Mount("/api", apiRouter)` for modular route organization - Use `chi.URLParam(r, "id")` to extract path parameters from the request context - Enable request ID tracking and structured logging with built-in middleware ## Key Features - Zero dependencies beyond the Go standard library - 100% compatible with net/http Handler and HandlerFunc interfaces - Inline middleware groups let you apply auth or rate limiting to specific route sets - Supports regex constraints on URL parameters for input validation at the router level - Efficient radix tree routing with minimal allocations per request ## Comparison with Similar Tools - **Gorilla Mux** — more features like host matching and query routing, but slower and heavier; Chi is faster and more composable - **Gin** — full framework with JSON binding and validation built in; Chi stays minimal and stdlib-compatible - **Echo** — batteries-included framework; Chi is a router-first library you compose yourself - **httprouter** — slightly faster raw routing but lacks middleware chaining and subrouter support - **Fiber** — uses fasthttp instead of net/http, breaking stdlib compatibility; Chi keeps full Go ecosystem interop ## FAQ **Q: Is Chi a web framework or just a router?** A: Chi is primarily a router with middleware support. It intentionally avoids bundling templating, ORM, or validation, letting you pick your own libraries. **Q: How does Chi compare in performance to Gin or Echo?** A: Chi performs within a few percent of Gin and Echo on routing benchmarks. The difference is negligible for most real applications where business logic dominates latency. **Q: Can I use Chi with existing net/http middleware?** A: Yes. Any middleware with the signature `func(http.Handler) http.Handler` works directly with Chi's `Use()` method. **Q: Does Chi support HTTP/2 and TLS?** A: Chi sits on top of net/http, so it inherits full HTTP/2 and TLS support from Go's standard server. ## Sources - https://github.com/go-chi/chi - https://go-chi.io/ --- Source: https://tokrepo.com/en/workflows/1f62c989-40c3-11f1-9bc6-00163e2b0d79 Author: Script Depot