Where to put “defer req.Body.Close()”?

前端 未结 5 819
你的背包
你的背包 2021-01-01 15:25

I have net/http handlers that have defer req.Body.Close() in each on web server side.

What is the correct place to put this in? Should I pu

5条回答
  •  情话喂你
    2021-01-01 15:46

    As mentioned in the documentation, no need to explicit close it both in client and server side.

    // Body is the request's body. 
    // 
    // For client requests, a nil body means the request has no 
    // body, such as a GET request. The HTTP Client's Transport 
    // is responsible for calling the Close method. 
    // 
    // For server requests, the Request Body is always non-nil 
    // but will return EOF immediately when no body is present. 
    // The Server will close the request body. The ServeHTTP 
    // Handler does not need to. 
    

提交回复
热议问题