如何在Golang中订阅多个频道

时间:2018-03-12 13:59:01

标签: go

它使用golang的频道。 如何使st2在以下代码中显示属性:

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)

这里的问题是: 它只有BTC-NEO工作。看起来BTC-ETC只是被忽略了。 如何订阅它们?

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

2 个答案:

答案 0 :(得分:3)

您可以使用2018-03-12 21:00:58.966215+0300 Diskon[261:8180] [DYMTLInitPlatform] platform initialization successful 2018-03-12 21:00:59.241857+0300 Diskon[261:8089] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-03-12 21:00:59.242550+0300 Diskon[261:8089] [MC] Reading from public effective user settings. 2018-03-12 21:00:59.371628+0300 Diskon[261:8089] [MC] Lazy loading NSBundle MobileCoreServices.framework 2018-03-12 21:00:59.374741+0300 Diskon[261:8089] [MC] Loaded MobileCoreServices.framework 2018-03-12 21:00:59.497213+0300 Diskon[261:8172] 4.8.1 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40009000 started 2018-03-12 21:00:59.497706+0300 Diskon[261:8172] 4.8.1 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled 2018-03-12 21:00:59.812086+0300 Diskon[261:8172] <Google> Advertising tracking may be disabled. To get test ads on this device, enable advertising tracking. 2018-03-12 21:00:59.837680+0300 Diskon[261:8089] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x107526090 V:[GADBannerView:0x1019fa3b0]-(5)-[UILabel:0x107521d90'%'] (active)>", "<NSLayoutConstraint:0x1075261e0 UILabel:0x107521d90'%'.centerY == UILabel:0x107520070'Kalkulator Diskon'.centerY (active)>", "<NSLayoutConstraint:0x107526400 UILabel:0x107521d90'%'.lastBaseline == UILabel:0x107520070'Kalkulator Diskon'.lastBaseline (active)>", "<NSLayoutConstraint:0x1075266a0 V:[GADBannerView:0x1019fa3b0]-(5)-[UILabel:0x107520070'Kalkulator Diskon'] (active)>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x107526090 V:[GADBannerView:0x1019fa3b0]-(5)-[UILabel:0x107521d90'%'] (active)> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2018-03-12 21:00:59.978231+0300 Diskon[261:8089] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x107555f20> should have parent view controller:<ViewController: 0x101877a00> but requested parent is:<UIInputWindowController: 0x102874400>' *** First throw call stack: (0x181173164 0x1803bc528 0x1811730ac 0x18a7c2804 0x18b2f74bc 0x18b2f85a0 0x18a73b92c 0x18b2f82fc 0x18b2f025c 0x18a7e1eec 0x18a753994 0x18a752fbc 0x18aaef974 0x18b3a124c 0x18a7ad8c4 0x18a98dc7c 0x18a992a14 0x18ac2e2a8 0x18af06100 0x18ac2df0c 0x18ac2e76c 0x18b3b37c0 0x18b3b3664 0x18b1212cc 0x18b2ba3cc 0x18b12117c 0x18af05760 0x18a991158 0x18ada6dbc 0x1838361f0 0x18383eaf8 0x100d1928c 0x100d259e4 0x18386a7f8 0x18386a49c 0x18386aa38 0x18111b77c 0x18111b6fc 0x18111af84 0x181118b5c 0x181038c58 0x182ee4f84 0x18a7915c4 0x1009309d4 0x180b5856c) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 语句在单个gouroutine中等待多个通道:https://play.golang.com/p/1PUfSyG0HHZ

更多链接:

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

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

答案 1 :(得分:0)

最后我发现解决方案是设置第一个SubscribeExchangeUpdate 所以

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