doujia2090 2019-03-26 16:40
浏览 43

如何在Go中选择?

In Unix select is used to wait for more than one input source. Select waits until one input source becomes ready.

How to do this in Go?

I found a Select in Go but this seams to be a thin wrapper around the Unix function, because it works on file descriptors.

How to wait for more than one connection, in particular UnixConn connections used for Unix Domain Sockets?

  • 写回答

1条回答 默认 最新

  • drxrgundk062317205 2019-03-26 17:53
    关注
    package main
    
    import (
        "fmt"
    )
    
    type Message struct {
        Payload int
    }
    
    func main() {
        var inA *Message
        var inB *Message
    
        rxA := make(chan *Message)
        rxB := make(chan *Message)
    
        go func(txA chan *Message){
          txA <- &Message{Payload: 1} 
        }(rxA)
    
        go func(txB chan *Message){
          txB <- &Message{Payload: 2} 
        }(rxB)
    
        for {
            select {
                case inA = <- rxA:
                case inB = <- rxB:
            }
            if inA != nil && inB != nil {
                fmt.Println(inA.Payload + inB.Payload)
                break
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败