一次选择和多个案例

use*_*291 4 concurrency go

当 C1 和 C2 都已收到时如何调整下面的代码来做某事 https://gobyexample.com/select

import "time"
import "fmt"

func main() {

    c1 := make(chan string)
    c2 := make(chan string)

    go func() {
        time.Sleep(time.Second * 1)
        c1 <- "one"
    }()
    go func() {
        time.Sleep(time.Second * 2)
        c2 <- "two"
    }()

    for i := 0; i < 2; i++ {
        select {
        case msg1 := <-c1:
            fmt.Println("received", msg1)
        case msg2 := <-c2:
            fmt.Println("received", msg2)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 5

这可能是一种管道技术,称为扇入

一个函数可以从多个输入中读取并继续进行,直到所有输入都关闭,方法是将输入通道多路复用到一个通道上,当所有输入都关闭时,该通道才关闭。这称为扇入。

func merge(cs ...<-chan int) <-chan int {
    var wg sync.WaitGroup
    out := make(chan string)

    // Start an output goroutine for each input channel in cs.  output
    // copies values from c to out until c is closed or it receives a value
    // from done, then output calls wg.Done.
    output := func(c <-chan string) {
        for n := range c {
            select {
            case out <- "received " + n:
            case <-done:
            }
        }
        wg.Done()
    }
    wg.Add(len(cs))
    for _, c := range cs {
        go output(c)
    }

    // Start a goroutine to close out once all the output goroutines are
    // done.  This must start after the wg.Add call.
    go func() {
        wg.Wait()
        close(out)
    }()
    return out
}
Run Code Online (Sandbox Code Playgroud)

在这个操场上看到一个完整的例子


注意:无论您最终将使用什么解决方案,都值得一读:

与渠道设计围棋的API的原则艾伦·什里夫

特别是:

原则#1

一个API应该声明其信道的方向