forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
546 B
30 lines
546 B
package main
|
|
|
|
import "fmt"
|
|
|
|
func consumer(ch chan int, done chan string) {
|
|
for i := range done {
|
|
fmt.Printf("------消息:%s\n", i)
|
|
}
|
|
done <- "ok"
|
|
}
|
|
func product(ch chan int) {
|
|
for i := 0; i < 5; i++ {
|
|
fmt.Printf("生产:%d\n", i)
|
|
ch <- i
|
|
}
|
|
close(ch)
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println("开始测试!")
|
|
ch := make(chan int)
|
|
done := make(chan string)
|
|
go consumer(ch, done)
|
|
go product(ch)
|
|
|
|
// 为了让主进程等待消费者结束
|
|
<-done // 注释:会导致第一个生成无法调用
|
|
// <-ch // 注释:不影响逻辑
|
|
|
|
}
|
|
|