```go
package main
import (
"time"
"fmt"
)
func main() {
c := make(chan interface{},100)
go Consumer{1}.Run(c)
go Consumer{2}.Run(c)
go Consumer{3}.Run(c)
go Productor{1}.Run(c)
go Productor{2}.Run(c)
go Productor{3}.Run(c)
select {}
}
type Consumer struct{
No int // 消费者编号
}
func (this Consumer)Run(c chan interface{}) {
for {
msg := <- c
fmt.Printf("消费者【%d】号,消费了%v\n",this.No,msg)
}
}
type Productor struct{
No int // 生产者编号
}
func (this Productor)Run(c chan interface{}) {
for i:=0;;i++ {
time.Sleep(100 * time.Millisecond)
c<-fmt.Sprintf("来自【生产者%d】的消息:%d",this.No,i)
}
}
```
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)