Search⌘ K

Solution Review: Devise Random Bit Generator

Explore how to implement a random bit generator in Go by leveraging goroutines and channels. Learn how to use select statements for concurrency control and to produce random bits continuously through proper channel communication.

We'll cover the following...
Go (1.6.2)
package main
import (
"fmt"
)
func main() {
ch := make(chan int)
// consumer:
go func() {
for {
fmt.Print(<-ch, " ")
}
}()
// producer:
for i:=0; i<=100000; i++ {
select {
case ch <- 0:
case ch <- 1:
}
}
}

In the code above, at line 7, we make a channel ch for integers. Then at line 9, we start an anonymous ...