mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
44 lines
525 B
Go
Executable File
44 lines
525 B
Go
Executable File
// goroutines2.go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
// integer producer:
|
|
func numGen(start, count int, out chan<- int) {
|
|
for i := 0; i < count; i++ {
|
|
out <- start
|
|
start = start + count
|
|
}
|
|
close(out)
|
|
}
|
|
|
|
// integer consumer:
|
|
func numEchoRange(in <-chan int, done chan<- bool) {
|
|
for num := range in {
|
|
fmt.Printf("%d\n", num)
|
|
}
|
|
done <- true
|
|
}
|
|
|
|
func main() {
|
|
numChan := make(chan int)
|
|
done := make(chan bool)
|
|
go numGen(0, 10, numChan)
|
|
go numEchoRange(numChan, done)
|
|
|
|
<-done
|
|
}
|
|
|
|
/* Output:
|
|
0
|
|
10
|
|
20
|
|
30
|
|
40
|
|
50
|
|
60
|
|
70
|
|
80
|
|
90
|
|
*/
|