dongqie2028
2018-03-12 13:59
浏览 98
已采纳

如何在Golang中订阅多个频道

It use the golang's channel. How to make st2 to display propertly in the following code:

package main

import (
   "fmt"
   "github.com/OpinionatedGeek/go-bittrex"
)

func main() {
 bt := bittrex.New("", "")
 ch := make(chan bittrex.ExchangeState, 16)
 go func() {
    for st := range ch1 {
        fmt.Println("Message:", st)
    }
    for st2 := range ch2 {
        fmt.Println("Message:", st2)
    }
}()

bt1.SubscribeExchangeUpdate("BTC-NEO", ch1, nil)
bt2.SubscribeExchangeUpdate("BTC-ETC", ch2, nil)

The problem here is that: it only BTC-NEO working. it looks BTC-ETC is just ignored. How to subscribe both of them?

the reference link is at: https://github.com/OpinionatedGeek/go-bittrex/blob/master/ws.go

图片转代码服务由CSDN问答提供 功能建议

它使用golang的频道。 如何使st2在以下代码中正确显示: \ n

 程序包main 
 
import(
“ fmt” 
“ github.com/OpinionatedGeek/go-bittrex"
)

func main(){
 bt:=  bittrex.New(“”,“”)
 ch:= make(chan bittrex.ExchangeState,16)
 go func(){
 for st:= range ch1 {
 fmt.Println(“ Message:”  ,st)
} 
 for st2:=范围ch2 {
 fmt.Println(“ Message:”,st2)
} 
}()
 
bt1.SubscribeExchangeUpdate(“ BTC-NEO”,ch1  ,nil)
bt2.SubscribeExchangeUpdate(“ BTC-ETC”,ch2,nil)
   
 
 

此处的问题是: it只有BTC-NEO可以工作。 如何同时订阅这两者?

参考链接位于: ”> https://github.com/OpinionatedGeek/go-bittrex/blob/master/ws.go \ n

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsqbkh3630 2018-03-12 22:39
    已采纳

    finally I found the solution is to set go for the first SubscribeExchangeUpdate so

    go bt1.SubscribeExchangeUpdate("BTC-NEO", ch1, nil)
    bt2.SubscribeExchangeUpdate("BTC-ETC", ch2, nil)
    
    打赏 评论
  • dongmaobeng7145 2018-03-12 14:11

    You can wait for multiply channels in single gouroutine using select statement: https://play.golang.com/p/1PUfSyG0HHZ

    More links:

    https://tour.golang.org/concurrency/5

    https://golang.org/ref/spec#Select_statements

    打赏 评论

相关推荐 更多相似问题