| 23 | } |
| 24 | |
| 25 | func (wp *workerPool) run() int { |
| 26 | numWords := 0 |
| 27 | // start the workers in the background and wait for data on the channel |
| 28 | // we already know the number of workers, we can increase the WaitGroup once |
| 29 | wp.wg.Add(wp.numWorkers) |
| 30 | for i := 0; i < wp.numWorkers; i++ { |
| 31 | go func() { |
| 32 | defer wp.wg.Done() |
| 33 | wordsDetected := wp.detectWords() |
| 34 | wp.mu.Lock() |
| 35 | defer wp.mu.Unlock() |
| 36 | numWords = numWords + wordsDetected |
| 37 | }() |
| 38 | } |
| 39 | |
| 40 | // wait for the workers to stop processing and exit |
| 41 | wp.wg.Wait() |
| 42 | return numWords |
| 43 | } |
| 44 | |
| 45 | // getMessages gets a slice of messages to process |
| 46 | func getMessages() []string { |