Skip to content

Instantly share code, notes, and snippets.

@jnovikov
Created May 5, 2019 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnovikov/82a345443fd70dd989311ef50055da17 to your computer and use it in GitHub Desktop.
Save jnovikov/82a345443fd70dd989311ef50055da17 to your computer and use it in GitHub Desktop.
Read files
package main
import (
"fmt"
"os"
"sync"
)
var m = make(map[string]int)
var wg sync.WaitGroup
func countWords(fileName string) {
f, _ := os.Open(fileName)
defer f.Close()
defer wg.Done()
s := ""
for n, e := fmt.Fscan(f, &s); n > 0 && e == nil; n, e = fmt.Fscan(f, &s) {
m[s] += 1
}
}
func main() {
files := []string{"text.txt", "/etc/passwd", "test.txt"}
wg.Add(len(files))
for _, v := range files {
countWords(v)
}
wg.Wait()
fmt.Println(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment