Last Updated: May 22, 2026
context.WithTimeout is the version of WithCancel to use when "cancel when I say so" becomes "cancel if this takes longer than X". It wraps a parent context and starts a timer; when the timer fires, the new context is cancelled automatically and ctx.Err() reports context.DeadlineExceeded. This chapter covers how that timer behaves, why you still need defer cancel(), and how to race a slow operation against the timeout using select.