liveBook · Manning

Context package

As explained in Learn Go with Pocket-Sized Projects

The context package is Go's standard-library package providing context types and functions for cancellation, deadlines, and request-scoped values.

The Go context package coordinates cancellation, deadlines, and request-scoped values across function and network-call boundaries. Create one application-root Context with context.Background() in main, then pass it explicitly to dependencies. section 10.6.2 section 10.8

Use contexts consistently:

  • Accept context.Context as the first function parameter and commonly name the parameter ctx; functions receiving a context should not replace it with Background() or TODO(). section 10.6.2
  • Derive children with context.WithTimeout for a duration, WithDeadline for a timestamp, or WithCancel when callers need explicit cancellation. section 10.6.2 section 10.8
  • Always bound remote calls with a deadline or timeout so an unavailable dependency cannot cause the application to wait forever. section 10.6.2 section 10.8
  • Select on ctx.Done() to detect expiration and inspect ctx.Err(), or context.Cause(ctx), to determine why the context ended. section 10.8
  • Treat WithValue as a last resort for request metadata rather than using Context as general application key-value storage. section 10.6.3 section 10.8

Bound a database call

func Create(ctx context.Context, db habitCreator, h Habit) (Habit, error) { dbCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) defer cancel() if err := db.Add(dbCtx, h); err != nil { return Habit{}, fmt.Errorf("cannot save habit: %w", err) } return h, nil } A backend can return promptly when the deadline fires: select { case response := <-responseChan: return response, nil case <-ctx.Done(): return nil, ctx.Err() }

Test the timeout path

A mock dependency can wait longer than the configured database timeout, then select on ctx.Done(). The expected result is context.DeadlineExceeded, making the cancellation behavior executable and testable.

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains root and child contexts, timeout propagation, cancellation detection, values, and timeout testing without detailing implementation internals.
Examples
Demonstrates a timed database write, channel-based deadline handling, and a mock test expecting context.DeadlineExceeded.
What it assumes
Assumes Go function calls, interfaces, channels, goroutines, and dependency mocking while teaching context creation and propagation.
What you can do after
Diagnose and bound remote operations by propagating contexts, selecting on Done(), and testing deadline failures.
How it connects
Connects Context to cancellation, deadlines, timeouts, remote calls, goroutines, dependency injection, mocking, and request metadata.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.