package main

import (
	"fmt"
	"math/rand"
	"time"
)

//生产者
func producer(header string,channel chan<- string)  {
	for{
		channel <- fmt.Sprintf("%s: %v",header,rand.Int31())
		time.Sleep(time.Second)
	}
}

//消费者
func customer(channel <- chan string)  {
	for  {
		message := <- channel
		fmt.Println(message)
	}
}

func main() {
	channel := make(chan string)
	go producer("火柴人",channel)
	go producer("木头人",channel)
	customer(channel)
}

 

版权声明:本文为dwxt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dwxt/p/12889757.html