Land your first dev job — 100% money-back guarantee.
Continue

Context in Go

Cancellation, deadlines, and request-scoped values.

Context in Go

The `context` package provides a standard way to carry **cancellation signals**, **deadlines**, and **request-scoped values** across API boundaries and goroutines. Well-behaved Go services accept a `context.Context` as their first argument, pass it down to other calls, and stop work promptly when the context is cancelled or times out.

1. Context Basics: Background and TODO

Root contexts you build from.

2. WithCancel: Manual Cancellation

Cancel work when you are done or an error occurs.

3. WithTimeout and WithDeadline

Stop work automatically after a deadline.

4. Context Values

Attach request-scoped metadata (not for business data).

5. HTTP Server Handlers with Context

Propagate cancellation and deadline through HTTP requests.

6. Best Practices and Pitfalls

How to use context correctly in large codebases.