In Go you time out or cancel a channel operation by adding a case to select. A timeout comes from time.After or a time.Timer, and cancellation comes from a context.Context whose Done() channel closes. Use a timeout when you only care about elapsed time, and a context when the caller, a parent operation or a deadline may cancel the work, which is the idiomatic choice in current Go.
time.After(d) returns a channel that receives one value after d. Putting it in a select bounds how long you wait for another channel.
Note that the worker goroutine above is still running and will block forever on the unbuffered result channel. A timeout stops waiting, it does not stop the work. Make result buffered (make(chan string, 1)) or give the worker a cancellation signal so it can exit.
Since Go 1.23, unreferenced timers are garbage collected even before they fire, and timer channels are unbuffered, so calling time.After in a loop no longer leaks memory as it did in older versions. If you support older toolchains, or need to reuse or stop a timer, use time.NewTimer and call Stop.
A context.Context carries a cancellation signal and an optional deadline across goroutines and API boundaries. When it is cancelled, ctx.Done() closes and every goroutine selecting on it wakes up.
ctx.Err() returns context.DeadlineExceeded after a timeout and context.Canceled after an explicit cancel(). Always call cancel, usually with defer, so the context releases its resources.
| Aspect | time.After or Timer | context |
|---|---|---|
| Trigger | Elapsed time only | Time, explicit cancel or a parent being cancelled |
| Reaches other goroutines | Only the one that selects | Every goroutine holding the context |
| Passes through function calls | Manual | Standard first parameter ctx |
| Works with standard library | Limited | net/http, database/sql, os/exec and more |
| Reason for stopping | None | ctx.Err() and context.Cause |
Derive a shorter context from the request context, so the operation obeys both limits.
A send can block too, so protect it with a select as the worker above does. Otherwise a goroutine that tries to send to a reader that has gone away leaks.
For several goroutines that must stop together, use golang.org/x/sync/errgroup with errgroup.WithContext: the first error cancels the shared context.
cancel function returned by context.WithTimeout, which go vet reports.context.Background() deep inside a function instead of the caller's context, so cancellation never arrives.ctx.Done() in a long loop that does not block on a channel. Check ctx.Err() regularly.No. It only stops the waiting side. The worker must watch ctx.Done() or write to a buffered channel so it can finish.
On Go 1.23 and later, yes, because unused timers are collected. On older versions each call keeps a timer alive until it fires, so use a reusable time.Timer.
Use WithTimeout for a duration from now and WithDeadline for a fixed point in time. They behave the same after creation.
Call ctx.Err(). Use context.WithCancelCause and context.Cause (Go 1.20 and later) when you need a custom reason.
Add a timeout or ctx.Done() case to select to stop waiting on a channel. time.After is enough for a single bounded wait, while context is the standard way to propagate cancellation, deadlines and reasons across goroutines and libraries. Make sure the work itself also stops, not just the wait.