0PricingLogin
Go Academy · Lesson

Setting Timeouts and Headers

Client timeouts, custom headers and auth tokens

Client-level timeout

Set http.Client.Timeout to bound the total time for request+response. This is the easiest timeout to configure.

client := &http.Client{Timeout: 10 * time.Second}

Transport-level timeouts

Fine-tune connection establishment and TLS handshake timeouts via http.Transport:

t := &http.Transport{
    DialContext: (&net.Dialer{
        Timeout:   5 * time.Second,
        KeepAlive: 30 * time.Second,
    }).DialContext,
    TLSHandshakeTimeout:   5 * time.Second,
    ResponseHeaderTimeout: 5 * time.Second,
}
client := &http.Client{Transport: t, Timeout: 30 * time.Second}

All lessons in this course

  1. Making HTTP Requests
  2. Setting Timeouts and Headers
  3. Decoding JSON Responses
  4. Error Handling and Retry Logic
← Back to Go Academy