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 request lifecycle signals through a gRPC service and its repository calls.
- How deep it goes
- Explains interface methods, context creation, propagation, per-call timeouts, and timeout testing without implementing the interface.
- Examples
- Uses a habit-creation repository call with a 100 ms timeout and a mock that waits for cancellation.
- What you can do after
- Apply it via Go’s context package to propagate contexts, bound repository calls, and test timeout behavior.
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
- Uses contexts to propagate deadlines, cancellation signals, and values across Go API boundaries.
- How deep it goes
- Explains the interface and cancellation notification, then works through cancellation-aware channel operations and context-lifetime mistakes.
- Examples
- Uses a file watcher and asynchronous HTTP-to-Kafka publishing to illustrate cancellation and context lifetime.
- What you can do after
- Diagnose context-lifetime bugs and apply cancellation-aware channel operations using Go's context package.
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.
What to know first
- GoGo is a statically typed, compiled, general-purpose programming language with garbage collection and built-in support for concurrency.
- channelA channel is a medium or logical pathway for conveying information, messages, or other content between sources and recipients.
- deadlineA deadline is the latest specified time by which a task, operation, or obligation must or is expected to be completed.
- handlerA handler is a software routine or component invoked to process an event, request, message, or other input.
Related concepts
- contractkind of · A contract is an agreed specification of the conditions, expectations, rights, or obligations that govern behavior or interactions between parties or software components.
- task cancellationuses · Task cancellation is a request or mechanism that signals an in-progress task or operation to stop before completing 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 software that mediates communication or processing between components, often by intercepting, transforming, or coordinating operations in a pipeline.
- Causeused by · context.Cause is a Go function that returns the specific error cause associated with a context's cancellation, or its cancellation error when no cause was set.
- Context.Donehas part · Context.Done is a method that returns a channel closed when a context is canceled or reaches its deadline.
Questions about context
What is context?
In Go, context.Context is an interface carrying cancellation signals, deadlines, and request-scoped values across function and service boundaries.
Which Manning books explain context?
Learn Go with Pocket-Sized Projects, Go by Example, Build and Orchestrator in Go (From Scratch), 100 Go Mistakes and How to Avoid Them, Go in Action, Second Edition, gRPC Microservices in Go and Go in Practice, Second Edition.
What should I know before learning context?
Manning books that teach context usually introduce Go, channel, deadline and handler first.
What is context a kind of?
Context is a kind of contract. A contract is an agreed specification of the conditions, expectations, rights, or obligations that govern behavior or interactions between parties or software components.
What are the parts of context?
Context includes Context.Done.
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.






