Skip to content

Instantly share code, notes, and snippets.

@gsora
Created March 3, 2016 19:19
Show Gist options
  • Select an option

  • Save gsora/b7dc758d19fbdff9789c to your computer and use it in GitHub Desktop.

Select an option

Save gsora/b7dc758d19fbdff9789c to your computer and use it in GitHub Desktop.
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