how to select for input on a dynamic list of channels in Go?
Question
Go has a mechanism to do a blocking read from one of several channels, the select statement. So you can say
select {
case <- c1:
case <- c2:
}
will block until we get input from either of these two channels. Very nice.
But this requires that I specify in the source code how many channels I want to poll. What if I have a slice or array of channels and I want to block until I get input on any of them?
Solution
Just a thought, but you could use a multiplexing pattern, where you spawn off a goroutine with 2 channels that blocks on both and sends the output to a new channel. Then you can just build up a tree of these dynamically from your list that funnels everything down to a single channel, which you then read on.
OTHER TIPS
Since go1.1, there's a proper API to dynamically do select sets.
Here's a complete and usable example:
package main
import (
"log"
"reflect"
)
func sendToAny(ob int, chs []chan int) int {
set := []reflect.SelectCase{}
for _, ch := range chs {
set = append(set, reflect.SelectCase{
Dir: reflect.SelectSend,
Chan: reflect.ValueOf(ch),
Send: reflect.ValueOf(ob),
})
}
to, _, _ := reflect.Select(set)
return to
}
func recvFromAny(chs []chan int) (val int, from int) {
set := []reflect.SelectCase{}
for _, ch := range chs {
set = append(set, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch),
})
}
from, valValue, _ := reflect.Select(set)
val = valValue.Interface().(int)
return
}
func main() {
channels := []chan int{}
for i := 0; i < 5; i++ {
channels = append(channels, make(chan int))
}
go func() {
for i := 0; i < 10; i++ {
x := sendToAny(i, channels)
log.Printf("Sent %v to ch%v", i, x)
}
}()
for i := 0; i < 10; i++ {
v, x := recvFromAny(channels)
log.Printf("Received %v from ch%v", v, x)
}
}
You can play around with it interactively on the playground
package main
import "fmt"
func main() {
c1 := make(chan int)
c2 := make(chan int)
go func() { c1 <- 1 }()
go func() { c2 <- 2 }()
cs := []chan int{c1, c2}
cm := make(chan [2]int)
for idx, c := range(cs) {
go func(idx int, c chan int) {
cm <- [2]int{idx, <-c}
}(idx, c)
}
fmt.Print(<-cm)
fmt.Print(<-cm)
}
prints [0 1][1 2]
(or maybe [1 2][0 1]
).
Perhaps something like this may apply?
// multiplex takes a slice of chan ints and returns a channel
// that multiplexes between all of them.
func multiplex(chs []<-chan int) <-chan int {
c := make(chan int)
d := make(chan bool)
for _, ch := range chs {
go func(ch <-chan int) {
for r := range ch {
c <- r
}
d <- true
}(ch)
}
go func() {
for i := 0; i < len(chs); i++ {
<-d
}
close(c)
}()
return c
}