DEV Community

Livio Ribeiro
Livio Ribeiro

Posted on

Discuss: Return a response or write to the response?

In most web frameworks (Spring, Django, FastAPI, Axum) the handlers return an object to the framework that will be used to build the response that is sent to the client.

However, the Go lang net/http package uses a different approach: the handlers write data directly into the response and have no return value.

I believe that this behavior comes from a time that the most frameworks worked with blocking IO and employed optimizations using queues and threads dedicated to process the responses, so your code would return an object describing the response and the framework would put it in the queue to be processed by a thread.

With non-blocking IO, there is no need for these optimizations, since the async runtime already takes care of everything related to IO, allowing handlers to write data directly to the response.

So, which is better? Return a response or write to the response?

Top comments (0)