★ 题目:使用映射(map)实现WordCount
https://tour.go-zh.org/moretypes/23
练习:映射
实现 WordCount。它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。
你会发现 strings.Fields 很有帮助。
★ 代码
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
m := make(map[string]int)
for _, word := range words {
n, ok := m[word]
if ok == false {
m[word] = 1
} else {
m[word] = n + 1
}
}
return m
}
func main() {
wc.Test(WordCount)
}
运行结果:
$ ./word_count.exe
PASS
f("I am learning Go!") =
map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
PASS
f("A man a plan a canal panama.") =
map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}
对代码做小的改动,运行结果跟上面一样,如下:
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
m := make(map[string]int)
for _, word := range words {
_, ok := m[word] // 更改了这里,不再赋给临时变量
if ok == false {
m[word] = 1
} else {
m[word]++ // 更改了这里,直接对m[word]的值加1
}
}
return m
}
func main() {
wc.Test(WordCount)
}