Explain `context.Context` and its primary uses.

Updated Apr 28, 2026

Short answer

context provides a way to pass deadlines, cancellation signals, and request-scoped values across API boundaries.

Deep explanation

In a concurrent server, a request might spawn multiple goroutines. If the client disconnects or a timeout is reached, you want to cancel all downstream work. Passing a context.Context down the call stack allows you to call a cancel function at the top level, propagating a Done() channel signal to all child operations.

Real-world example

Canceling expensive database queries in PostgreSQL if the user closes their browser before the HTTP request finishes loading.

Common mistakes

  • Passing large amounts of operational data or dependencies through `context.WithValue`. Context should only hold request-scoped metadata (like trace IDs), not application state.

Follow-up questions

  • Can you modify a context once it is created?

More Golang interview questions

View all →