package main

import (

"fmt"
"sync"

)

type messagequeue struct {

queue chan string

}

// 创建一个消息队列
func newqueue(size int) *messagequeue {

return &messagequeue{
    make(chan string, size)}

}

// 生产者
func (mq *messagequeue) producequeque(msg string) {

fmt.Println("生产者生产的", msg)
mq.queue <- msg

}

// 消费者
func (mq messagequeue) consumerqueque(wg sync.WaitGroup) {

defer wg.Done()
for m := range mq.queue {
    fmt.Println("消费者消费", m)
}

}

func main() {

var wg sync.WaitGroup

mq := newqueue(10)
wg.Add(1)
go func() {
    defer wg.Done()
    for i := 0; i < 5; i++ {
        msg := fmt.Sprintf("使用的数据的%d", i)
        mq.producequeque(msg)
    }
    close(mq.queue)
}()
wg.Wait()
wg.Add(1)
go mq.consumerqueque(&wg)
wg.Wait()

}