From 8b525e218e94f25e7e5023ac7a32192eefedce38 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 7 Jul 2017 16:13:45 +0200 Subject: [PATCH] Improved word count --- file-wordcount/main.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 }