package main
import (
"fmt"
)
func generator(c chan int) {
for i := 0; i < 5; i++ {
c <- i
}
}
func main() {
var c chan int
c = make(chan int)
go generator(c)
for i := 0; i < 5; i++ {
fmt.Printf("You say: %d\n", <-c)
}
//注意如果直接使用for的话, 会报错 fatal error: all goroutines are asleep - deadlock!
//建议使用公用的计数器.
// for {
// fmt.Printf("You say: %d\n", <-c)
// }
fmt.Println("You're boring; I'm leaving.")
close(c)
}