How to release http.Client in Go?

I built a connection http.Clientfor HTTP2, what do I need to do to free up the client and resource used?

Thank.

+2
source share
1 answer

http.ClientIt does not require a special way to free up "used" resources. When it becomes unavailable, the memory used by it will be returned by the garbage collector.

http.ClientDoes not store connection or status information. The documentation even states that it http.Clientshould be reused:

( TCP-), , . .

, (, ) http.Client, , , Close() , , , Close() .

:

, , http.Client HTTP- (, Client.Do(), Client.Get(), Client.Post() ..), *http.Response, , , , Response.Body.Close(). doc http:

, :

resp, err := http.Get("http://example.com/")
if err != nil {
  // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...

Client.Get():

err nil, resp non-nil resp.Body. Caller resp.Body, .

+8

All Articles