All topics / chi From Zero

chi From Zero

Learn chi, the lightweight idiomatic Go router: why it stays compatible with net/http, routing and URL params, sub-routers, the stdlib-style middleware stack, reading and writing requests with the standard library, building a REST API, and structuring and testing it. The framework that's barely a framework — and the clearest bridge to plain net/http.

  1. What chi Is chi is a lightweight idiomatic Go router that stays 100% compatible with net/http: handlers and middleware are plain stdlib types. Install it, write a tiny server, and meet the philosophy.
  2. Routing, URL Params & Sub-routers How chi maps method plus pattern to plain http.HandlerFuncs, reads URL params with chi.URLParam, and groups related routes with Route, Mount, and nested sub-routers.
  3. Middleware the Standard Way Middleware in chi is plain net/http: a func(http.Handler) http.Handler that wraps the next handler. Learn to write one, register with Use and With, use chi's built-ins, and pass data via context.
  4. Requests & Responses with the Standard Library chi gives you a router and nothing else for I/O — so you read JSON with encoding/json, write it with a tiny helper, and set headers, status, and body in the right order.
  5. Building a REST API Assemble routing, middleware, and stdlib JSON I/O into full CRUD for the articles resource — five plain http.HandlerFuncs over a mutex-guarded in-memory store, mounted on a chi sub-router.
  6. Structuring & Testing Grow the articles API into packages with a dependency-holding Handler struct, pass request-scoped values through context safely, and test the whole router with net/http/httptest.
  7. Where to Go Next Look at what you can build now, see honestly where chi sits next to Gin, Echo, and the improved Go 1.22 net/http mux, add a real database, and take the articles API home.