As correctly mentioned in many other answers here and elsewhere, ResponseWriter
is an interface and the implications of this have beed described in detail in SO answers and blogs.
What I would like to address is what I feel is the big—and dangerous—misconception here, that the reason request is passed by "reference" (although such a thing does not really exist in Go) is that "we want to make changes to it visible to the server".
Quoting a couple of answers:
[..] it's just a struct, and since we want to change this struct and have the web server see those changes, it has to be a pointer [..] SO
[..] changes to Request by the handler need to be visible to the server, so we’re only passing it by reference instead of by value [..] SO
This is wrong; in fact the docs explicitly warn against tampering with / mutating the request:
Except for reading the body, handlers should not modify the provided Request.
Quite the opposite, no? :-)
If you want to change the request, e.g. append a tracing header before passing it on to the next handler in a middleware chain you have to copy the request and pass the copied version down the chain.
Requests to change the behavior to allow modifications of the incoming request have been raised with the Go team but changing something like this would probably lead to at least some existing code breaking unexpectedly.
Why use a pointer if we are explicitly telling people not to mutate the request? Performance, Request
is a large struct and copying it can bring performance down, especially with long middleware chains in mind. The team had to strike a balance, definitely not an ideal solution, but the tradeoffs are clearly on the side of performance here (instead of API safety).