package main
import "fmt"
func randGenerator() chan int {
ch := make(chan int)
go func() {
for {
//select會嘗試執行各個case,如果都可以執行,那麼隨機選一個執行
select {
case ch <- 0:
case ch <- 1:
case ch <- 2:
case ch <- 3:
case ch <- 4:
case ch <- 5:
case ch <- 6:
case ch <- 7:
case ch <- 8:
case ch <- 9:
}
}
}()
return ch
}
func main() {
generator := randGenerator()
//測試,列印10個隨機數
for i := 0; i < 10; i++ {
fmt.Println(<-generator)
}
}