映射:练习
实现 WordCount。它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。
```go
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordcount :=make(map[string]int)
for _, word := range words{
elem,ok := wordcount[word]
if ok == false{
wordcount[word] = 1
}else{
wordcount[word] = elem + 1
}
}
return wordcount
}
func main() {
wc.Test(WordCount)
}