The Default HTTP Client Has No Timeout Gotcha

Web
The default Go HTTP client has no timeout, so you must explicitly set a Timeout on the client or use a context to prevent hanging requests.

The default http.Client has no timeout, causing requests to hang indefinitely if the server does not respond. You must explicitly set a Timeout on the client or use a context with a deadline to prevent this.

client := &http.Client{
	Timeout: 30 * time.Second,
}
resp, err := client.Get("https://example.com")

Alternatively, use a context for per-request control:

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", "https://example.com", nil)
resp, err := http.DefaultClient.Do(req)