http.ServeMux
The request router in Go's net/http package, which matches each request's path, and optionally its method, to the handler registered for it.
In more detail
Create one with http.NewServeMux() and register handlers with HandleFunc, such as mux.HandleFunc("/health", health). Since Go 1.22 patterns can include a method and wildcards, such as GET /items/{id}. Using your own mux keeps routing explicit instead of relying on a global default.
Example
mux := http.NewServeMux()
mux.HandleFunc("/health", health)Related terms
- http.Handler — In Go, a function or type that receives an HTTP request and writes the response, with the signature func(w http.ResponseWriter, r *http.Request).
- http.Server — The Go type that listens on a network address and serves HTTP requests through a handler, with fields for limits such as timeouts.
- endpoint — An address that a program sends requests to, such as a web path like /health or a network socket.
Taught in
- Serving HTTP with net/http Server — Concurrent Systems Programming with Go