Go

What does this print?

Question 263HardGo 1.22 to 1.25
func main() {
	parent, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
	defer cancel()

	child, cancelChild := context.WithCancel(parent)
	defer cancelChild()

	<-child.Done()
	fmt.Println(child.Err())
	fmt.Println(errors.Is(child.Err(), context.DeadlineExceeded))
	fmt.Println(context.Cause(child) == context.DeadlineExceeded)

	cancelChild()
	fmt.Println(child.Err())
}

Output:

context deadline exceeded
true
true
context deadline exceeded

The child is a plain WithCancel, but when the parent times out, propagation cancels the child with the parent's error and cause, which is DeadlineExceeded. So the child reports "deadline exceeded" even though it has no timer of its own. The later cancelChild() does nothing: cancellation is first-write-wins, so Err() never changes once set.

What the interviewer is looking for: knowing that the error comes from whichever cancellation happened first anywhere up the chain, and that Err() is fixed after the first cancel.

More on Context

All 35 Context questions