Created
July 18, 2012 19:38
-
-
Save jordanorelli/3138363 to your computer and use it in GitHub Desktop.
a channel of functions
This file contains hidden or 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" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
rand.Seed(time.Now().Unix()) | |
rand.Int31n(4) | |
c := make(chan func() int) | |
go func() { | |
for { | |
var fn func() int | |
switch rand.Int31n(4) { | |
case 0: | |
fn = func() int { | |
return 0 | |
} | |
case 1: | |
fn = func() int { | |
return 1 | |
} | |
case 2: | |
fn = func() int { | |
return 2 | |
} | |
case 3: | |
fn = func() int { | |
return 3 | |
} | |
} | |
c <- fn | |
} | |
}() | |
for fn := range c { | |
fmt.Println(fn()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment