A Tour of Go 40 Exercise: Maps

演習:WordCount 関数を実装して、文字列をカウント。

 WordCountの仕様が最初問題読んでも良く解らんかった。
実行結果のテストから仕様理解したけどw;

package main

import (
    "code.google.com/p/go-tour/wc"
    "strings"
)

func WordCount(s string) map[string]int {
    ss := strings.Fields(s)
    m := make(map[string]int)
 	  
    for _, str := range ss {
     	c, ok := m[str]
        if ok {
            m[str] = c + 1

        } else {
            m[str] = 1
        }
    }
    return m
}

func main() {
    wc.Test(WordCount)
}