Created
March 3, 2016 19:19
-
-
Save gsora/b7dc758d19fbdff9789c to your computer and use it in GitHub Desktop.
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 ( | |
| crypto "crypto/rand" | |
| "fmt" | |
| "math/big" | |
| "time" | |
| ) | |
| func main() { | |
| producer := make(chan int64) | |
| // producer | |
| go func() { | |
| for { | |
| var bigN *big.Int | |
| val := *big.NewInt(4) | |
| bigN, _ = crypto.Int(crypto.Reader, &val) | |
| time.Sleep(time.Duration(bigN.Int64()) * time.Second) | |
| newN, _ := crypto.Int(crypto.Reader, &val) | |
| producer <- newN.Int64() | |
| } | |
| }() | |
| // the consumer is the main itself | |
| for { | |
| select { | |
| case got := <-producer: | |
| fmt.Println("Producer sent in something: ", got) | |
| case <-time.After(3 * time.Second): | |
| fmt.Println("you're too slow, feed me faster!") | |
| return | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment