Created
May 5, 2019 10:16
-
-
Save jnovikov/82a345443fd70dd989311ef50055da17 to your computer and use it in GitHub Desktop.
Read files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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