Go services · 3 MIN READ
An HTTP request has a lifecycle and a budget
Separate request parsing, domain work, downstream deadlines and response writing.
A Go web service is more than a route callback. Every request consumes a connection, parsing work, application time and possibly downstream capacity. Give those resources explicit lifetimes. This lesson describes native server code; guest Go programs in the app cannot open network sockets.
Keep the boundary small
A handler translates a protocol into an application operation: validate input, call a service with the request context, then map the result to a response. Domain logic should not need a ResponseWriter. If the client leaves, the request context can notify cooperative downstream work.
Configure a server rather than only a callback
This is a native-server configuration fragment. Header and idle timeouts address specific connection phases; they are not a complete business-operation deadline. WriteTimeout must fit the response type, especially streaming, rather than being copied blindly.
srv := &http.Server{
Addr: ":8080",
Handler: handler,
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 60 * time.Second,
}
// Start srv and arrange graceful shutdown in the owning process.The values are illustrative. Add request-body limits, operation-specific deadlines and response policies based on the API. A timeout does not undo an operation the remote service already committed.
Choose HTTP when it matches the problem
HTTP APIs fit request/response interactions and interoperability. A batch program or local library may need no server at all. Test handlers with httptest and in-memory requests before deployment. In-memory handler tests check application behavior; they do not prove production TLS, socket, proxy or load-balancer configuration.
TAKE THIS WITH YOU
A handler is an adapter with explicit resource limits. Pass context into the application and test protocol behavior without requiring a running network service.
Check your understanding
Which statement is correct about an http adapter?
Go to the source
Progress stays in this browser. No account needed.