The answer is how context.Context
and time.Timer
deliver the signal (cancel).
context.Context
gives you access to the channel using the Context.Done()
method , which will be closed when the goroutines used should complete.
time.Timer
gives you access to the channel in the Timer.C
struct field , along which the value will be sent after a specified period of time (this value will be the current time, but it does not matter here).
Key points are highlighted there. The closure of the channel can be observed by any number of horus and an infinite number of times. Spec: Receive statement:
A receive operation on a closed channel can always begin immediately, giving the element type a value of zero after all previously sent values ββhave been received.
Thus, a Context
can be used to suppress the signal to an arbitrary number of goroutines and places. A Timer
can only be used to signal one target, one who receives a value from its channel. If several clients are listening or trying to get from their channel, only one of them will be lucky to receive it.
Also, if you use / work with code that already supports / expects context.Context
, then this is not a question that should be used. Go 1.8 also added additional context support . There were significant additions to the database/sql
package with context support; including DB.BeginTx()
:
The provided context is used until the transaction is completed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided by BeginTx is canceled.
This is the main use of context.Context
: transferring the deadline and suppressing the signal along the API boundaries, and this is done in a safe way at the same time (since Context
values ββare immutable and channels are also safe for simultaneous use, data schedules cannot occur, by design, moreover: If I use channels correctly, should I use mutexes? ).
Related Blog Entries:
Go Go Blog: Go Concurrency Templates: Context
Context value traps and how to eliminate or reduce them in Go
Dave Cheney: context for cancellation