Go's standard library is enough to build production web services: net/http provides the server, router and client, encoding/json handles request and response bodies, context carries deadlines and cancellation, and log/slog records requests. Since Go 1.22, http.ServeMux supports methods and path parameters, so many APIs no longer need a third-party router.
This guide builds a small JSON API, then covers timeouts, middleware, graceful shutdown, an HTTP client and testing.
net/httphttp.Clientnet/httpThe Go 1.22 mux accepts patterns such as GET /items/{id} and reads the value with r.PathValue.
Requests with the wrong method receive 405 Method Not Allowed automatically. Limit body size with http.MaxBytesReader, and validate the decoded data before using it. The map is guarded by a mutex because handlers run concurrently, one goroutine per request.
http.ListenAndServe uses a server with no timeouts, which lets slow clients hold connections open. Create an http.Server and set them explicitly.
For HTTPS use srv.ListenAndServeTLS(cert, key). HTTP/2 is enabled automatically over TLS.
Middleware is a function that wraps an http.Handler.
Chain them as recoverPanic(logging(mux)). Use the same pattern for authentication, rate limiting, CORS and request IDs. Pass per-request values through r.Context(), not globals.
Stop accepting new connections and let in-flight requests finish when the process receives a signal.
This matters on Kubernetes and other orchestrators, which send SIGTERM before stopping a container.
http.ClientThe default client has no timeout. Build your own, reuse it, and tie each request to a context.
Always close the response body, or connections cannot be reused.
net/http/httptest lets you test a handler without opening a port.
Not necessarily. Since Go 1.22 the standard mux handles methods and path parameters. Frameworks still help with large APIs that want built-in binding and validation helpers.
Register a pattern like /items/{id} and call r.PathValue("id") in the handler.
Without them, slow or stalled clients can hold connections and goroutines indefinitely. ReadHeaderTimeout is the minimum to set.
Pick a consistent JSON error shape, set the right status code, and never leak internal error text to clients.
Go's standard library gives you a router, server, client, JSON codec, context, logging and test tools. Use method-based patterns, explicit server timeouts, middleware, graceful shutdown and a client with a timeout, and you have a solid base for web services and APIs.