0Pricing
Go Academy · Lesson

Setting Timeouts and Headers

Client timeouts, custom headers and auth tokens

Setting Timeouts and Headers is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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}

Context-based timeout

Context timeouts are per-request and propagate to downstream calls. They cancel the request when the deadline passes.

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)

Setting request headers

Add headers to a request with req.Header.Set (replaces) or req.Header.Add (appends):

req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Request-ID", requestID)

Common headers

Content-Type for request bodies; Accept for desired response format; Authorization for auth tokens; User-Agent for client identification.

Reading response headers

Read response headers from resp.Header.Get(key):

contentType := resp.Header.Get("Content-Type")
retryAfter := resp.Header.Get("Retry-After")

Idle connection timeout

Set IdleConnTimeout on the transport to close idle connections, preventing them from accumulating when traffic drops.

t.IdleConnTimeout = 90 * time.Second
t.MaxIdleConnsPerHost = 10

TLS configuration

Customise TLS (minimum version, client certificates) via tls.Config on the transport:

t.TLSClientConfig = &tls.Config{MinVersion: tls.VersionTLS12}

Detecting timeout errors

Distinguish timeout errors from network errors using errors.As:

var urlErr *url.Error
if errors.As(err, &urlErr) && urlErr.Timeout() {
    // handle timeout
}

Per-request vs client timeout

Client.Timeout applies to every request. A context timeout is per-request and can be shorter than the client timeout. Both are respected — the shorter wins.

Disable keep-alive

Set DisableKeepAlives: true on the transport for short-lived programs that open few connections, avoiding the overhead of connection pooling.

Quick Check

When both http.Client.Timeout and a context timeout are set, which takes effect?

Recap: Timeouts and Headers

Key points:

  • client.Timeout: total per-request timeout
  • context.WithTimeout: cancellable, propagating timeout
  • req.Header.Set for custom headers
  • Transport for fine-grained TLS, dial, and idle timeouts

Frequently asked questions

Is the “Setting Timeouts and Headers” lesson free?

Yes — the full text of “Setting Timeouts and Headers” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Setting Timeouts and Headers”?

Client timeouts, custom headers and auth tokens You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Setting Timeouts and Headers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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