In Go HTTP handlers, why is the ResponseWriter a value but the Request a pointer?

前端 未结 5 1310
耶瑟儿~
耶瑟儿~ 2020-12-04 17:12

I\'m learning Go by writing an app for GAE, and this is signature of a handler function:

func handle(w http.ResponseWriter, r *http.Request) {}
5条回答
  •  我在风中等你
    2020-12-04 17:44

    What you get for w is a pointer to the non exported type http.response but as ResponseWriter is an interface, that's not visible.

    From server.go:

    type ResponseWriter interface {
        ...
    }
    

    On the other hand, r is a pointer to a concrete struct, hence the need to pass a reference explicitly.

    From request.go:

    type Request struct {
        ...
    }
    

提交回复
热议问题