mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
29 lines
391 B
Go
Executable File
29 lines
391 B
Go
Executable File
// prod_cons.go
|
|
/* producer-consumer problem in Go */
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
var done = make(chan bool)
|
|
var msgs = make(chan int)
|
|
|
|
func produce() {
|
|
for i := 0; i < 10; i++ {
|
|
msgs <- i
|
|
}
|
|
done <- true
|
|
}
|
|
|
|
func consume() {
|
|
for {
|
|
msg := <-msgs
|
|
fmt.Print(msg, " ")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
go produce()
|
|
go consume()
|
|
<-done
|
|
}
|
|
// Output: 0 1 2 3 4 5 6 7 8 9 |