#Understanding http.ResponseWriter

2 messages · Page 1 of 1 (latest)

wise sapphire
#

Hi guys, just started with go and working on my first "hello world" program. I created a webservice with a get endpoint using net/http and writing a handler. The handler interface looks as follows:

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

While reading about whether structs in go are pass by reference or pass by value, I came to the conclusion (please correct me if I am wrong), that a struct is in general passed by value. Thus, when I change a 'basic' data type like int, bool or even an array of the struct inside a function, that change won't propagate to the struct outside of the function (in the scope of the calling function). An exception to this rule would be, that the struct itself has a pointer to a value. Because then even inside the calling function, we have a reference to the address-space where the value of the struct is stored and can directly modify it.

Now coming back to the ResponseWriter, I noticed that while we have a pointer to the http.request, we do not have a pointer to the ResponseWriter. So at my current understanding, whatever struct is actually passed to ServeHTTP as the first argument, must have a pointer to the response/connection, because otherwise my responsewriter.Write would not have any impact on the actual response. Is that assumption correct?

dense pilot
#

Hey, the difference is that Request is a struct, while ResponseWriter is an interface. The value passed by http as a first argument to ServeHTTP will be a pointer to a struct (unexported).