Created
October 28, 2011 16:05
-
-
Save emepyc/1322637 to your computer and use it in GitHub Desktop.
goroutines filling a channel in the order they are fired
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" | |
"runtime" | |
"time" | |
) | |
func init() { | |
runtime.GOMAXPROCS(4) | |
} | |
func send(ch chan int, i int) { | |
time.Sleep(2e9) | |
ch <-i | |
} | |
func main() { | |
ch := make(chan chan int, 4) | |
for _,i := range([]int{0,1,2,3}) { | |
chval := make(chan int) | |
ch <- chval | |
go send(chval, i) | |
} | |
close(ch) | |
for sch := range(ch) { | |
select { | |
case v := <-sch : | |
fmt.Println(v) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment