Implement WordCount
. It should return a map of the counts of each “word” in the string s
. The wc.Test
function runs a test suite against the provided function and prints success or failure.
You might find strings.Fields helpful.
package main import ( "code.google.com/p/go-tour/wc" "strings" ) func WordCount(s string) map[string]int { m := make(map[string]int) strs := strings.Fields(s) for _,value := range strs { v, ok := m[value] if !ok { m[value] = 1 }else{ m[value] = v + 1 } } return m } func main() { wc.Test(WordCount) }
package main import ( "code.google.com/p/go-tour/wc" "strings" ) func WordCount(s string) map[string]int { m := make(map[string]int) strs := strings.Fields(s) for i := 0; i < len(strs); i++ { v, ok := m[strs[i]] if !ok { m[strs[i]] = 1 }else{ m[strs[i]] = v + 1 } } return m } func main() { wc.Test(WordCount) }