funcfanIn(chans ...<-chaninterface{}) <-chaninterface{} { out := make(chaninterface{})
gofunc() { var wg sync.WaitGroup wg.Add(len(chans)) for _, ch := range chans { gofunc(ch <-chaninterface{}) { for v := range ch { out <- v } wg.Done() }(ch) } // Wait for all forwarders before closing out wg.Wait() close(out) }() return out }
WaitGroup avoids closing out while a goroutine might still send (out <- v), which would panic.
Recursive version
Split inputs in half and merge recursively. mergeTwo uses a nil channel so select blocks reads when a side is done.
funcfanInRecur(chans ...<-chaninterface{}) <-chaninterface{} { switchlen(chans) { case0: c := make(chaninterface{}) close(c) // Nothing to merge: closed channel (receive OK, send blocked) return c case1: return chans[0] case2: return mergeTwo(chans[0], chans[1]) default: // Halve and recurse m := len(chans) / 2 return mergeTwo( fanInRecur(chans[:m]...), fanInRecur(chans[m:]...)) } }
funcmergeTwo(a, b <-chaninterface{}) <-chaninterface{} { c := make(chaninterface{}) gofunc() { deferclose(c) for a != nil || b != nil { // while at least one side is readable select { case v, ok := <-a: if !ok { // a closed — nil blocks this case a = nil continue } c <- v case v, ok := <-b: if !ok { // b closed — nil blocks this case b = nil continue } c <- v } } }() return c }
Reflect version
Build reflect.SelectCase slices for a multi-case receive loop.
funcfanInReflect(chans ...<-chaninterface{}) <-chaninterface{} { out := make(chaninterface{}) gofunc() { deferclose(out) // Build select cases var cases []reflect.SelectCase for _, c := range chans { cases = append(cases, reflect.SelectCase{ Dir: reflect.SelectRecv, Chan: reflect.ValueOf(c), }) }
// Loop until every channel is drained forlen(cases) > 0 { i, v, ok := reflect.Select(cases) if !ok { // Channel closed — drop from cases cases = append(cases[:i], cases[i+1:]...) continue } out <- v.Interface() } }() return out }
Benchmarks:
Fan-out
Synchronous version
Send each value to every output in turn; close all outputs when the input closes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcfanOut(ch <-chaninterface{}, out []chaninterface{}) { gofunc() { deferfunc() { // Close all outputs on exit for i := range out { close(out[i]) } }()
for v := range ch { // Read input v := v for i := range out { i := i out[i] <- v // Blocking send to each output } } }() }
Async goroutine version
Spawning a goroutine per send avoids blocking the reader on slow outputs—but watch shutdown:
funcfanOut(ch <-chaninterface{}, out []chaninterface{}) { gofunc() { deferfunc() { // Close all outputs on exit for i := range out { close(out[i]) } }()
for v := range ch { // Read input v := v for i := range out { i := i // Async send gofunc(){} out[i] <- v }() } } }() }
On exit you may close outputs while a send goroutine still runs; each out[i] needs a reader or the send blocks. A WaitGroup waits for in-flight sends before close:
funcfanOutAsync(ch <-chaninterface{}, out []chaninterface{}) { gofunc() { var wg sync.WaitGroup deferfunc() { // Close outputs after all sends finish wg.Wait() for i := range out { close(out[i]) } }()
for v := range ch { // Read input v := v for i := range out { i := i wg.Add(1) gofunc() { // One slow output must not block others out[i] <- v wg.Done() }() } } }() }
Reflect version
Pre-build send cases; after a successful send, set that case’s channel to nil so it cannot fire again until the next value.
funcfanOutReflect(ch <-chaninterface{}, out []chaninterface{}) { gofunc() { deferfunc() { // Close all outputs on exit for i := range out { close(out[i]) } }() cases := make([]reflect.SelectCase, len(out)) // Build send cases for i := range cases { cases[i].Dir = reflect.SelectSend } for v := range ch { v := v // Refresh Send values for i := range cases { cases[i].Chan = reflect.ValueOf(out[i]) cases[i].Send = reflect.ValueOf(v) } // One select per output for this value forrange cases { chosen, _, _ := reflect.Select(cases) // Nil blocks re-selection until next round cases[chosen].Chan = reflect.ValueOf(nil) } } }() }