Go's standard library is enough to build production web services. Use net/http for servers, routing and clients, html/template for safe HTML rendering, net/url for URL handling, encoding/json for APIs, net/http/httptest for testing and log/slog for logging. Since Go 1.22, http.ServeMux supports method matching and path wildcards, so a router package is optional.
This guide shows a complete JSON service with routing, middleware, server timeouts and graceful shutdown, then covers templates, cookies and testing.
Patterns can include an HTTP method and {name} wildcards. Read wildcard values with r.PathValue.
A request with the wrong method receives 405 Method Not Allowed automatically, with an Allow header. The more specific pattern wins when several match. In versions before Go 1.22 only a path prefix could be matched, which is why older tutorials check r.Method by hand.
This example handles a JSON body, validates it, returns proper status codes and wires up middleware.
Try it:
The key points are the body size limit, strict decoding, a RWMutex protecting the shared map (handlers run concurrently), middleware as a function that wraps an http.Handler, and an explicit http.Server with timeouts. Calling http.ListenAndServe directly creates a server with no timeouts.
html/template escapes output by context, so user input cannot inject scripts. Parse templates once at startup.
A request to /hello?name=<b>x</b> prints the tags as text rather than bold markup. Use text/template only for non-HTML output such as emails or configuration files.
Set HttpOnly, Secure and SameSite on session cookies. Build URLs with url.Values so values are escaped correctly.
Use one shared http.Client with a timeout, http.NewRequestWithContext, and always close the response body. http.DefaultClient has no timeout.
httptest exercises handlers without a network port:
Test through the mux, not the handler alone, so path wildcards are populated.
Often not. With method and wildcard routing in ServeMux, many services only need the standard library. Routers still help with route groups, built-in middleware chains and convenience binding.
http.ListenAndServe uses a zero-value http.Server. Create your own server and set ReadHeaderTimeout and the other timeouts.
Declare it in the pattern, such as GET /items/{id}, and call r.PathValue("id") in the handler (Go 1.22 and later).
Call Server.Shutdown(ctx) when you receive a signal. It stops accepting new connections and waits for active requests until the context expires.
Build web and HTTP solutions with net/http using method and wildcard patterns, an explicit http.Server with timeouts, middleware functions, encoding/json with size limits, and html/template for safe HTML. Reuse one http.Client with a timeout, set secure cookie flags, test with httptest, and shut down gracefully.