Home > Backend Development > Golang > Why Can I Still Receive Values from a Closed Go Channel?

Why Can I Still Receive Values from a Closed Go Channel?

DDD
Release: 2024-12-09 17:42:12
Original
479 people have browsed it

Why Can I Still Receive Values from a Closed Go Channel?

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)
    }
}
Copy after login

Output:

0
1
2
3
4
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template