Why Receiving Values from a Closed Channel is Possible
In Go, channels provide a communication mechanism between goroutines. When a channel is closed, it indicates that no more values will be sent. However, it doesn't mean that existing values in the channel's buffer are immediately discarded.
The specification states that after closing a channel, any previously sent values can still be received before receiving the zero value. This is because the channel's buffer acts as a queue, storing values sent before the close operation.
How It Works:
Consider the following code:
package main import "fmt" import "sync" import "time" func main() { iCh := make(chan int, 99) var wg sync.WaitGroup go func() { for i := 0; i < 5; i++ { wg.Add(1) go func(i int) { defer wg.Done() iCh <- i }(i) } wg.Wait() close(iCh) }() time.Sleep(5 * time.Second) for i := range iCh { fmt.Printf("%v\n", i) } }
Output:
0 1 2 3 4
Even though the channel is closed after 5 seconds, the for loop still receives the 5 values that were sent before the close operation. This is because the channel buffer stores these values until they are consumed.
Only after receiving all previously sent values will the for loop return the zero value, indicating that the channel is closed.
Why the "Time.Sleep" Trick Fails:
The "time.Sleep" trick does not prevent the goroutines from continuing to send values into the channel before it is closed. While the goroutines are sleeping for 5 seconds, they could be sending additional values that would still be buffered and received by the for loop.
The above is the detailed content of Why Can I Still Receive Values from a Closed Go Channel?. For more information, please follow other related articles on the PHP Chinese website!