多路复用非常简单:创建要多路复用到的通道切片,启动一个goroutine,该例程从原始通道读取并将每个消息复制到该切片中的每个通道:
// Really this should be in Bootloader but this is just an examplevar consumers []chan []bytefunc (b *Bootloader) multiplex() { // We'll use a sync.once to make sure we don't start a bunch of these. sync.once(func(){ go func() { // Every time a message comes over the channel... for v := range b.logCh { // Loop over the consumers... for _,cons := range consumers { // Send each one the message cons <- v } } }() })}


