To time out a channel operation in Go, put it in a select with a second case that fires after a delay, either <-time.After(d) or, better, <-ctx.Done() from a context created with context.WithTimeout or context.WithDeadline. To cancel the work behind the channel, pass the context to the goroutine so it stops when the context ends.
Without a timeout, a receive from a channel that nobody writes to blocks forever and leaks the goroutine. This article shows the three tools and when to use each.
time.After(d) returns a channel that delivers one value after d. Combined with select, it limits how long you wait.
There is a bug waiting in this program. After the timeout, the worker goroutine still tries result <- "done" on an unbuffered channel that nobody reads, so it blocks forever. Make the channel buffered with make(chan string, 1) so the worker can always finish, or use a context so the worker can stop early.
In Go 1.23 and later, unreferenced timers are garbage collected even before they fire, so time.After in a loop no longer leaks memory the way it did in older releases. On older versions, create a time.NewTimer and call Stop to release it.
A context carries a deadline and a cancellation signal across goroutine and API boundaries. This is the standard Go solution and what net/http, database/sql and gRPC accept.
ctx.Err() returns context.DeadlineExceeded for a timeout and context.Canceled when cancel was called. Check them with errors.Is. Always call cancel, usually with defer, even when the timeout fires on its own.
WithTimeout(ctx, d) is shorthand for WithDeadline(ctx, time.Now().Add(d)). Use WithDeadline when you already have a fixed point in time, such as a request that must finish by a given moment.
Child contexts inherit the parent's deadline, and the earlier deadline wins. A request-scoped context with a 5 second limit therefore bounds every call you make with it.
A timeout in the caller does not stop the work unless the worker watches the context. A worker that loops should check ctx.Done() in each iteration:
The send is inside a select with ctx.Done(), so the goroutine exits promptly instead of blocking on a reader that has gone away.
| Situation | Use |
|---|---|
| One-off wait on a single channel | select with time.After |
| Cancelling nested calls or goroutines | context.WithTimeout |
| A fixed wall-clock cut-off | context.WithDeadline |
| Repeated per-iteration timeout | time.NewTimer with Reset |
| Network socket deadlines | SetDeadline on net.Conn |
For HTTP clients, set http.Client.Timeout or attach the context with http.NewRequestWithContext so the whole request, including reading the body, is cancelled.
No. It only stops the waiting. Pass a context to the worker and check ctx.Done() so it can exit, and use a buffered result channel so its final send never blocks.
WithTimeout takes a duration from now, while WithDeadline takes an absolute time. They produce the same kind of context.
Yes. Calling cancel releases resources immediately and is safe to call more than once. Linters such as go vet warn when you forget it.
On Go 1.23 and later the garbage collector reclaims unfired timers, so it is fine for most code. On older versions, use a reusable time.Timer.
Time out a channel operation by adding a timer or ctx.Done() case to your select. Prefer context.WithTimeout or WithDeadline because they also cancel the work, always call cancel, and give result channels a buffer so abandoned goroutines can finish instead of leaking.