Go's standard library is enough to build production web services and APIs. The net/http package provides the server, client, and router, encoding/json handles request and response bodies, context carries deadlines, log/slog does structured logging, and net/http/httptest makes handlers testable. Since Go 1.22 the default ServeMux routes by HTTP method and path parameters, so a small API needs no third-party router.
The sections below build a complete JSON API step by step, then cover the client side, middleware, graceful shutdown, and design guidance.
ServeMuxnet/httphttp.ClientServeMuxPatterns can include an HTTP method and wildcards. r.PathValue reads a wildcard, and a request with a known path but the wrong method gets 405 Method Not Allowed automatically.
More specific patterns win over general ones, and {path...} matches the rest of a path. A {$} suffix matches a path exactly.
net/httpThe program below is a runnable in-memory API. It decodes JSON with a size limit, validates input, and returns proper status codes.
Try it:
Handlers run in their own goroutines, so shared state needs a lock, as the sync.RWMutex shows. In a real service the store is a database.
Middleware is a function that wraps an http.Handler. Use it for logging, authentication, request IDs, panic recovery, and CORS.
Compose several by nesting them, with the outermost running first.
http.ClientNever use http.DefaultClient for production calls, because it has no timeout. Build one client, reuse it so connections are pooled, and tie each request to a context.
Always close the body, check the status code, and wrap errors with %w so callers can use errors.Is and errors.As.
Set timeouts on http.Server to defend against slow clients, and shut down on a signal so in-flight requests finish.
For HTTPS use ListenAndServeTLS or terminate TLS at a proxy. HTTP/2 is enabled automatically over TLS.
net/http/httptest lets you test a handler without opening a port:
| Need | Standard library | Add a package |
|---|---|---|
| Routing by method and path | ServeMux | Chi or Gin for route groups and built-in middleware |
| JSON | encoding/json | Faster or stricter codecs when profiling shows a need |
| Validation | Manual checks | A validation library for large schemas |
| API contracts | None | OpenAPI generators |
| RPC between services | JSON over HTTP | gRPC |
Not for most services. Since Go 1.22 the standard router handles methods and path parameters, and middleware is a plain function. Frameworks help with convenience and larger teams' conventions.
Use http.TimeoutHandler or check r.Context() inside the handler, and pass that context into database and HTTP calls.
Use the right status code and a consistent JSON body such as {"error": "..."}. Do not leak internal error text to clients, and log it on the server.
encoding/json fast enough?Usually. Profile first. A newer encoding/json/v2 is being developed, available as an experiment in recent Go releases, but v1 remains the stable default.
Go's net/http package, with the Go 1.22 router, encoding/json, context, and log/slog, is a complete toolkit for web services. Limit request bodies, set server and client timeouts, wrap handlers in middleware, shut down gracefully, and test with httptest. Add third-party packages only when a concrete need appears.