package main

import (
	"fmt"
	"sync"
)

var messages = [][]string{
	{

		"Horatio says 'tis but our fantasy",
		"And will not let belief take hold of him",
		"Touching this dreaded sight, twice seen of us",
		"Therefore I have entreated him along",
		"With us to watch the minutes of this night",
		"That if again this apparition come",
		"He may approve our eyes and speak to it",
	},
	{
		"Last night of all",
		"When yond same star that's westward from the pole",
		"Had made his course to illume that part of heaven",
		"Where now it burns, Marcellus and myself",
		"The bell then beating one",
	},
	{
		"Sit down awhile",
		"And let us once again assail your ears",
		"That are so fortified against our story",
		"What we have two nights seen",
	},
	{
		"Peace, break thee off; look, where it comes again!",
	},
}

const producerCount int = 4
const consumerCount int = 3

func produce(link chan<- string, id int, wg *sync.WaitGroup) {
	defer wg.Done()
	for _, msg := range messages[id] {
		link <- msg
	}
}

func consume(link <-chan string, id int, wg *sync.WaitGroup) {
	defer wg.Done()
	for msg := range link {
		fmt.Printf("Message \"%v\" is consumed by consumer %v\n", msg, id)
	}
}

func main() {
	link := make(chan string)
	wp := &sync.WaitGroup{}
	wc := &sync.WaitGroup{}

	wp.Add(producerCount)
	wc.Add(consumerCount)

	for i := 0; i < producerCount; i++ {
		go produce(link, i, wp)
	}

	for i := 0; i < consumerCount; i++ {
		go consume(link, i, wc)
	}

	wp.Wait()
	close(link)
	wc.Wait()
}
