Context
In Go, context.Context is an interface carrying cancellation signals, deadlines, and request-scoped values across function and service boundaries.
Learn Go with Pocket-Sized Projects
context.Context is a Go interface carrying cancellation signals, deadlines, and request-scoped values across function and service boundaries.
- How it is used
- Carries cancellation and deadlines from a gRPC endpoint through business logic to repository calls.
- How deep it goes
- Defines the four interface methods and works through child-context creation, timeout handling, and tests without implementing the interface.
- Examples
- Uses a habit-creation repository call with a 100 ms timeout and a slow mock expecting context.DeadlineExceeded.
- What you can do after
- Apply it via Go’s context package to propagate request cancellation and test bounded repository calls.
Read how Learn Go with Pocket-Sized Projects explains context →
Go by Example
context.Context is a Go standard-library interface that carries cancellation signals, deadlines, and request-scoped values across call boundaries.
- How it is used
- Coordinates cancellation, timeouts, and request metadata across concurrent pipelines, HTTP requests, CLIs, middleware, and tests.
- How deep it goes
- Explains tree semantics, Done-based handling, timeout cleanup, HTTP propagation, and value middleware; defers full cancellation of every pipeline stage as an exercise.
- Examples
- Demonstrates a cancellable request pipeline, derived iterator Context, Ctrl+C-aware CLI, trace-ID middleware, and HTTP client test.
- What you can do after
- Apply it via the context package to stop concurrent work gracefully, propagate request cancellation, and carry informational trace IDs.
Build and Orchestrator in Go (From Scratch)
A Go context carries request-scoped values, deadlines, and cancellation signals across API and process boundaries.
- How it is used
- Passes request-scoped information into Docker client operations while running or stopping containers.
- How deep it goes
- Defines context and demonstrates `context.Background()` passed through Docker calls, without implementing deadlines, cancellation, or values.
- Examples
- Uses `context.Background()` for image pulling, container lifecycle calls, and a PostgreSQL container task.
- What you can do after
- Apply it via the Docker client by creating a background context and supplying it to container API calls.
Read how Build and Orchestrator in Go (From Scratch) explains context →
100 Go Mistakes and How to Avoid Them
context.Context is the Go interface defining methods for retrieving deadlines, cancellation state, and propagated values.
- How it is used
- Carries deadlines, cancellation signals, and values across Go API boundaries, including concurrent work.
- How deep it goes
- Defines the interface methods and works through cancellation-aware channel operations and an HTTP propagation failure.
- Examples
- Uses a cancelable file watcher, channel send-and-receive code, and asynchronous publication from an HTTP handler.
- What you can do after
- Diagnose/debug with it: identify blocking operations that ignore cancellation and background work interrupted by an inappropriate request context.
Read how 100 Go Mistakes and How to Avoid Them explains context →
Go in Action, Second Edition
A Go context is a request-scoped value carrying cancellation, deadlines, and other propagation metadata across call boundaries.
- How it is used
- Manages cancellation, deadlines, and request-scoped values across concurrent HTTP, database, and downstream operations.
- How deep it goes
- Demonstrates timeout creation, deferred cancellation, propagation, and errgroup derivation, without detailing Context internals.
- Examples
- Uses a 60-second request timeout, RSS feed fetching, ParseURLWithContext, and errgroup cancellation across concurrent fetches.
- What you can do after
- Apply context via the standard package to bound operations and cancel related concurrent work.
gRPC Microservices in Go
A Go context is a request-scoped value that carries cancellation, deadlines, and other metadata across call boundaries.
- How it is used
- Carries call time limits across gRPC services and tracing data into application logs.
- How deep it goes
- Explains deadline and timeout constructors, cancellation, downstream propagation, and span extraction through client and logging code; does not detail context internals.
- Examples
- Uses a one-second Shipping call, context propagation through Order and Product services, and a logrus formatter that adds trace IDs.
- What you can do after
- Apply it via Go context constructors and gRPC calls to bound waits, then use logrus context-aware logging to correlate operations.
Go in Practice, Second Edition
A Go context is a request- or operation-scoped value that carries deadlines, cancellation signals, and associated key-value data across function calls.
- How it is used
- Coordinates request lifetimes, cancellation, deadlines, and scoped state in concurrent code and HTTP servers.
- How deep it goes
- Demonstrates creation, cancellation, deadline handling, value propagation, and middleware integration through runnable HTTP examples.
- Examples
- Uses cancellable background contexts, user-agent middleware, timeout handlers, and PostgreSQL-backed HTTP handlers.
- What you can do after
- Operate HTTP request lifecycles, propagate scoped values, enforce deadlines, and diagnose discarded cancellation functions with go vet.
Related concepts
- contractkind of · An API contract is a specification of the operations, data, and behavioral expectations that govern interactions between software components.
- task cancellationuses · Task cancellation is a request or mechanism for stopping an asynchronous or in-progress operation before it completes normally.
- context packageproduced by · The context package is Go’s standard-library package for propagating cancellation, deadlines, timeouts, and request-scoped values across call boundaries.
- middlewareused by · Middleware is an intermediate software component or layer that processes or mediates requests, messages, or operations between other components, often adding behavior or controlling their flow.
- done channel patternuses · The done channel pattern uses a channel to signal cancellation or completion so concurrent operations can stop and perform cleanup.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






