Bagaimana Memecahkan Penyata Pilihan Apabila Semua Saluran Ditutup dalam Go?

Linda Hamilton
Lepaskan: 2024-11-13 16:47:02
asal
146 orang telah melayarinya

How to Break Out of a Select Statement When All Channels Are Closed in Go?

Breaking Out of a Select Statement When All Channels Are Closed

Consider a scenario where multiple goroutines send data to individual channels, and you wish to consume this data in an arbitrary order. Using a select statement offers a convenient way to handle concurrent input from multiple sources. However, the challenge arises when you need to exit the loop only when all channels are closed.

Naive Solution

An intuitive approach might involve checking the availability of each channel by using the ok flag. However, this solution is prone to errors because a closed channel becomes available again if it's selected. This leads to an infinite loop, as demonstrated in this playground example: http://play.golang.org/p/rOjdvnji49.

Improved Solution

A more effective approach is to utilize the fact that a nil channel is never ready for communication. Each time a channel closes, it can be set to nil, ensuring that it's excluded from future select statements. This guarantees that the loop only terminates when all channels have closed:

for {
    select {
    case x, ok := <-ch:
        fmt.Println("ch1", x, ok)
        if !ok {
            ch = nil
        }
    case x, ok := <-ch2:
        fmt.Println("ch2", x, ok)
        if !ok {
            ch2 = nil
        }
    }

    if ch == nil && ch2 == nil {
        break
    }
}
Salin selepas log masuk

Playground example: http://play.golang.org/p/8lkV_Hffyj

Practical Considerations

In practice, it's unlikely that you'll need to handle a large number of channels in a single select statement. If such a scenario arises, it's advisable to use a more specialized solution, such as a channel multiplexer, to avoid cluttering the code with excessive nil checks.

Atas ialah kandungan terperinci Bagaimana Memecahkan Penyata Pilihan Apabila Semua Saluran Ditutup dalam Go?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan