In this article from Go101 site I have read some trick with double selection for stopCh
channel (in section "2. One receiver, N senders, the only receiver says "please stop sending more" by closing an additional signal channel").
Could you please describe how it works and do I really need to use it in real-world applications?
UPD: I don't asked about channel close. I have asked about usage of this part of the code:
// The try-receive operation is to try
// to exit the goroutine as early as
// possible. For this specified example,
// it is not essential.
select {
case <- stopCh:
return
default:
}
// Even if stopCh is closed, the first
// branch in the second select may be
// still not selected for some loops if
// the send to dataCh is also unblocked.
// But this is acceptable for this
// example, so the first select block
// above can be omitted.
select {
case <- stopCh:
return
case dataCh <- rand.Intn(Max):
}
What is the real use case for double selection of stopCh
?