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).
In more detail
A ServeMux calls the right handler for each path, and the server runs each request's handler in its own goroutine. Keep handlers small: read the request, call your logic, write a status and body. They can be tested in memory with httptest.
Example
func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}Related terms
- http.ResponseWriter — The Go interface a handler uses to send its response: set headers, write the status code with WriteHeader, then write the body.
- 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.
- 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.
- httptest — Go's standard library package of helpers for testing HTTP code, such as a recorder that captures what a handler writes.
Taught in
- Panics and Recover — Concurrent Systems Programming with Go
- Serving HTTP with net/http Server — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go