Skip to content

Added worker version for Go #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion episode/9/episode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ notes:
- "partition method: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.partition"
errata:
- "[killercup](https://github.com/killercup) added [an alternative approach](https://github.com/hello-rust/show/pull/45) that creates one hashmap per file and uses Rayon's reduce to unify them."
- "[euantorano](https://github.com/euantorano) provided [an alternative Go approach to counting words](https://github.com/hello-rust/show/pull/46)."
metas:
- "Color scheme: JetJet Alternate for VSCode"
licenses:
Expand All @@ -46,4 +47,4 @@ markers:
- "Rust code: 9:38"
- "From sequential to concurrent code: 15:36"
- "The beauty of Rust: 23:23"
- "Handling errors in for_each: 28:05"
- "Handling errors in for_each: 28:05"
87 changes: 87 additions & 0 deletions episode/9/go/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
"sync"
)

func worker(tasks <-chan string, wordsChan chan<- string, errorsChan chan<- error, wg *sync.WaitGroup) {
defer wg.Done()

for filename := range tasks {
file, err := os.Open(filename)

if err != nil {
errorsChan <- err
continue
}

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)

for scanner.Scan() {
word := strings.ToLower(scanner.Text())

wordsChan <- word
}

if scanner.Err() != nil {
errorsChan <- scanner.Err()
}

file.Close()
}
}

func collectResults(m map[string]int, wordsChan <-chan string, errorsChan <-chan error) {
for {
select {
case word := <-wordsChan:
if _, ok := m[word]; ok {
m[word] += 1
} else {
m[word] = 1
}
case err := <-errorsChan:
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
}
}

func main() {
numArgs := len(os.Args) - 1

jobs := make(chan string, numArgs)
wordsChan := make(chan string, numArgs)
errorsChan := make(chan error, numArgs)

wordCount := make(map[string]int)
var wg sync.WaitGroup

wg.Add(runtime.NumCPU())

for w := 0; w < runtime.NumCPU(); w++ {
go worker(jobs, wordsChan, errorsChan, &wg)
}

go collectResults(wordCount, wordsChan, errorsChan)

for _, f := range os.Args[1:] {
jobs <- f
}

close(jobs)

wg.Wait()

fmt.Println("Words that appear more than once:")
for word, count := range wordCount {
if count > 1 {
fmt.Printf("%d %s\n", count, word)
}
}
}