R
R
Rouslan9432021-04-04 03:21:23
go
Rouslan943, 2021-04-04 03:21:23

Why in Go HTTP handlers is ResponseWriter a value and request a pointer?

why is the Request object a pointer and ResponseWriter not a pointer? Is there a need for this or is it just to make some advanced pointer based code possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Romses Panagiotis, 2021-04-04
@romesses

ResponseWriter is an interface.
Thus, the argument is the implementation of this interface (instance), which is still passed by pointer.

U
uvelichitel, 2021-04-04
@uvelichitel

ResponseWriter is an interface . It is not customary to pass an interface type to a function by reference UsePointer(* SomeInterface ) because such an argument is hard to construct. Even if your MyResponseWriter type satisfies the ResponseWriter interface, you would have to

MyOwnWriter := ResponseWriter(MyResponseWriter)
MyArgument := &MyOwnWriter
//и только потом
UsePointer(MyArgument)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question