36 lines
587 B
Go
36 lines
587 B
Go
package main
|
|
|
|
import "fmt"
|
|
import "io"
|
|
import "io/ioutil"
|
|
import "log"
|
|
import "os"
|
|
import "strings"
|
|
|
|
func wc(r io.Reader) map[string]uint {
|
|
words := make(map[string]uint)
|
|
if text, err := ioutil.ReadAll(r); err != nil {
|
|
log.Fatalln(err)
|
|
} else {
|
|
for _, word := range strings.Fields(string(text)) {
|
|
words[word]++
|
|
}
|
|
}
|
|
return words
|
|
}
|
|
|
|
func openFile(path *string) *os.File {
|
|
f, err := os.Open(os.Args[1])
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
return f
|
|
}
|
|
func main() {
|
|
if len(os.Args) <= 1 {
|
|
log.Fatalln("Missing file")
|
|
}
|
|
f := openFile(&os.Args[1])
|
|
fmt.Println(wc(f))
|
|
}
|