tezvyn:

What is Go's context package and how do you use WithCancel?

AI-drafted, machine-checkedSource: pkg.go.devintermediate

This tests cancellation propagation. A good answer says Context carries deadlines, signals; derive a child with WithCancel, pass it to worker, then call cancel to unblock ctx.Done. Bad: storing context in structs, leaking cancel functions, or ignoring Done.

WHAT THIS TESTS: This question evaluates whether you understand that Go's context package is primarily a mechanism for request-scoped cancellation and deadline propagation, not just a bag of values. The interviewer wants to see that you treat context as a control plane for goroutine lifecycles and that you know how to avoid goroutine leaks by respecting cancellation signals.

A GOOD ANSWER COVERS: First, the conceptual purpose: Context carries deadlines, cancellation signals, and request-scoped values across API boundaries and between goroutines. Second, propagation rules: the context should be the first parameter in functions, typically named ctx, and it must be passed explicitly rather than stored in structs. Third, the WithCancel pattern: you start with a parent context, derive a child context and a CancelFunc using context.WithCancel, pass the child to a goroutine, and call the CancelFunc to signal shutdown. Fourth, the worker implementation: the goroutine should use a select block that waits on ctx.Done() and exits cleanly when that channel closes, rather than blocking indefinitely or polling.

COMMON WRONG ANSWERS: Storing a context inside a struct type instead of passing it as a function argument. Ignoring the CancelFunc returned by WithCancel, which leaks the child context and any associated goroutines until the parent is canceled. Using context values as optional function parameters rather than strictly for request-scoped data that crosses API boundaries. Writing workers that ignore ctx.Done() and rely only on time.Sleep or uninterruptible blocking operations. Passing a nil context instead of context.TODO or context.Background.

LIKELY FOLLOW-UPS: How does cancellation propagate through a tree of derived contexts? The interviewer may ask you to explain that canceling a parent automatically cancels all children, but canceling a child does not affect the parent. What is the difference between WithDeadline and WithTimeout? WithTimeout is a convenience wrapper around WithDeadline that adds a duration to the current time. When should you use context values versus function parameters? Values are for request-scoped metadata like trace IDs; optional behavior belongs in explicit parameters. How do you handle cleanup when a context is canceled? Mention deferring the CancelFunc and using sync.WaitGroup to block until workers return.

ONE CONCRETE EXAMPLE: Imagine a main function that spawns a worker goroutine processing jobs from a channel. The main function creates ctx, cancel := context.WithCancel(context.Background()), then passes ctx to the worker. The worker runs a loop with select that checks ctx.Done() and a jobs channel. When the main function receives an interrupt signal, it calls cancel(). That closes the Done channel, the worker's select unblocks, the worker drains or abandons its current job and returns, and the main function can exit without leaking goroutines.

Read the original → pkg.go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.