Last active
August 29, 2015 14:26
-
-
Save sochoa/1d4234d6adedc805e58c to your computer and use it in GitHub Desktop.
Any ideas why this doesn't work?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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