What's the difference between timeout set in http.Client and timeout set in request's context?
I've seen 2 ways of setting timeout in http client.
First:
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
Second:
client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
when to use one over the other?