Created
June 6, 2014 21:10
-
-
Save bolilla/e63132fe8aeea4603cac to your computer and use it in GitHub Desktop.
GoPadawan Concurrencia Select Array
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 ( | |
"fmt" | |
"sync" | |
) | |
func multiplexar(inputs []chan int, output chan int) { | |
var group sync.WaitGroup | |
for i := range inputs { | |
group.Add(1) | |
go func(input <-chan int) { | |
for val := range input { | |
output <- val | |
} | |
group.Done() | |
}(inputs[i]) | |
} | |
go func() { | |
group.Wait() | |
close(output) | |
}() | |
} | |
func main() { | |
in := make([]chan int, 10) | |
for i := 0; i < len(in); i += 1 { | |
in[i] = make(chan int) | |
go func(c chan int, i int) { | |
c <- i | |
close(c) | |
}(in[i], i) | |
} | |
out := make(chan int) | |
multiplexar(in, out) | |
for x := range out { | |
fmt.Println("He leído", x) | |
} | |
} | |
//Extraído de: http://stackoverflow.com/a/10985364/1600421 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment