Skip to content

Instantly share code, notes, and snippets.

@sochoa
Last active August 29, 2015 14:26
Show Gist options
  • Save sochoa/1d4234d6adedc805e58c to your computer and use it in GitHub Desktop.
Save sochoa/1d4234d6adedc805e58c to your computer and use it in GitHub Desktop.
Any ideas why this doesn't work?
package main
import (
"sync"
"fmt"
"time"
)
func merge(inputs []chan interface{}) chan interface{} {
var group sync.WaitGroup
var output chan interface{}
for i := range inputs {
group.Add(1)
go func(input chan interface{}) {
for val := range input { output <- val }
group.Done()
} (inputs[i])
}
go func() {
group.Wait()
close(output)
} ()
return output
}
func main() {
var channels []chan interface{}
for x := 0; x < 3; x++ {
channels = append(channels, make(chan interface{}))
channels[x] <- x
close(channels[x])
}
merged := merge(channels)
loop:
for {
select {
case i := <-merged:
fmt.Println(i)
case <- time.After(3*time.Second):
fmt.Println("Breaking loop")
break loop
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment