Skip to content

Instantly share code, notes, and snippets.

@emepyc
Created October 28, 2011 16:05
Show Gist options
  • Save emepyc/1322637 to your computer and use it in GitHub Desktop.
Save emepyc/1322637 to your computer and use it in GitHub Desktop.
goroutines filling a channel in the order they are fired
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