2017-07-07 12:29:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
import "io"
|
|
|
|
import "log"
|
|
|
|
import "os"
|
2017-07-07 14:13:45 +00:00
|
|
|
import "bufio"
|
2017-07-07 12:29:36 +00:00
|
|
|
import "strings"
|
|
|
|
|
|
|
|
func wc(r io.Reader) map[string]uint {
|
|
|
|
words := make(map[string]uint)
|
2017-07-07 14:13:45 +00:00
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
s.Split(bufio.ScanWords)
|
|
|
|
for s.Scan() {
|
|
|
|
word := strings.ToLower(s.Text())
|
|
|
|
words[word]++
|
2017-07-07 12:29:36 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|