breaking out of a select statement when all channels are closed

前端 未结 5 1368
后悔当初
后悔当初 2021-01-30 00:48

I have two goroutines independently producing data, each sending it to a channel. In my main goroutine, I\'d like to consume each of these outputs as they come in, but don\'t ca

5条回答
  •  隐瞒了意图╮
    2021-01-30 01:32

    Why not use goroutines? As your channels are getting closed, the whole thing turns into a simple range loop.

    func foo(c chan whatever, prefix s) {
            for v := range c {
                    fmt.Println(prefix, v)
            }
    }
    
    // ...
    
    go foo(mins, "Min:")
    go foo(maxs, "Max:")
    

提交回复
热议问题