diff --git a/file-wordcount/main.go b/file-wordcount/main.go index 32e7ae0..1fbb17f 100644 --- a/file-wordcount/main.go +++ b/file-wordcount/main.go @@ -2,19 +2,18 @@ package main import "fmt" import "io" -import "io/ioutil" import "log" import "os" +import "bufio" 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]++ - } + s := bufio.NewScanner(r) + s.Split(bufio.ScanWords) + for s.Scan() { + word := strings.ToLower(s.Text()) + words[word]++ } return words }