package main

import (
	"fmt"
	"strings"
	"sync"
)

func parallelWordCount(texts []string) map[string]int {
	partials := make(chan map[string]int, len(texts))
	var wg sync.WaitGroup

	for _, text := range texts {
		text := text
		wg.Go(func() {
			local := make(map[string]int)
			for _, word := range strings.Fields(text) {
				local[word]++
			}
			partials <- local
		})
	}

	wg.Wait()
	close(partials)

	total := make(map[string]int)
	for partial := range partials {
		for word, count := range partial {
			total[word] += count
		}
	}
	return total
}

func main() {
	counts := parallelWordCount([]string{
		"go channels go",
		"mutex channels",
		"go waitgroup",
	})
	fmt.Println(counts["go"], counts["channels"], counts["mutex"])
}
