The Go documentation says
The Client's Transport typically has internal state (cached TCP connections), so Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.
Why does Client reuse help with preserving the Transport’s internal state, given that I can instantiate a transport and pass it to single-use Client instances like this:
client := &http.Client{Transport: transport}
There are many reasons. The most universal one is to allow reusing existing HTTP connections. If you use a new
http.Clientinstance for every request, you can never take advantage of existing HTTP connections, which will result in more overhead and slower performance for most common uses.Another common reason would be to use a cookie jar, if you're calling an HTTP server that uses cookies.