chi for Go: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is chi and why is it needed?

\n\n

chi is a lightweight, high-performance, and idiomatic HTTP router for the Go programming language. It is designed for developing web applications and APIs, providing developers with a powerful and flexible tool for routing HTTP requests.

\n\n

Unlike the standard net/http package, chi offers:

\n
    \n
  • Compatibility with net/http: chi fully implements the http.Handler interface, making it easy to integrate with existing Go code and middleware.
  • \n
  • Context-based routing: chi uses context.Context to pass route parameters (e.g., user ID) between middleware and handlers.
  • \n
  • Nested routes (Subrouters): Allows grouping routes into logical blocks (e.g., /api/v1/users).
  • \n
  • Middleware grouping: Middleware can be applied to the entire router or to individual route groups.
  • \n
  • Built-in support for CORS, logging, compression, and other common tasks.
  • \n
\n\n

chi is ideal for building REST APIs, microservices, and any web applications requiring a clean, performant, and easily testable architecture.

\n\n

Installing chi

\n\n

To install chi, use the standard Go package manager. Make sure you have Go installed (version 1.14 or higher).

\n\n
go get -u github.com/go-chi/chi/v5
\n\n

After running this command, the library will be added to your go.mod file. If you are using Go Modules (recommended), the import will look like this:

\n\n
import "github.com/go-chi/chi/v5"
\n\n

It is also common to install the middleware package:

\n\n
go get -u github.com/go-chi/cors\ngo get -u github.com/go-chi/chi/v5/middleware
\n\n

Quick Start with a Code Example

\n\n

Let's create a simple HTTP server that responds to GET requests.

\n\n
package main\n\nimport (\n    "fmt"\n    "net/http"\n\n    "github.com/go-chi/chi/v5"\n)\n\nfunc main() {\n    r := chi.NewRouter()\n\n    // Define a route\n    r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {\n        fmt.Fprintf(w, "Hello, chi!")\n    })\n\n    // Start the server\n    http.ListenAndServe(":3000", r)\n}
\n\n

Run this code and open your browser to http://localhost:3000/hello. You will see the message "Hello, chi!".

\n\n

Main chi Methods and Functions

\n\n

Let's look at the key methods of the chi library. All examples assume you have already created a router r := chi.NewRouter().

\n\n

1. Get(pattern string, handlerFn http.HandlerFunc)

\n

What it does: Registers a handler for the HTTP GET method at the specified path pattern.

\n

Parameters:

\n
    \n
  • pattern — a path string (e.g., /users, /users/{id}).
  • \n
  • handlerFn — a handler function matching the http.HandlerFunc signature.
  • \n
\n

Example:

\n
r.Get("/", func(w http.ResponseWriter, r *http.Request) {\n    w.Write([]byte("Home page"))\n})
\n\n

2. Post(pattern string, handlerFn http.HandlerFunc)

\n

What it does: Registers a handler for the HTTP POST method.

\n

Parameters: Similar to Get.

\n

Example:

\n
r.Post("/users", func(w http.ResponseWriter, r *http.Request) {\n    // Logic for creating a user\n    w.WriteHeader(http.StatusCreated)\n    w.Write([]byte("User created"))\n})
\n\n

3. Put(pattern string, handlerFn http.HandlerFunc)

\n

What it does:

Recommendations