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

前端 未结 5 1300
耶瑟儿~
耶瑟儿~ 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:52

    I think that the main reason for the Request object to be passed as a pointer is the Body field. For a given HTTP request, the body can only we read once. If the Request object was cloned, as it would be if it wasn't passed as a pointer, we would have two objects with different information about how much has been read from the body.

提交回复
热议问题