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 (
"tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
stringsplits := strings.Fields(s)
m := make(map[string]int)
for _, value := range stringsplits {
m[value] += 1
}
return m
}
func main() {
wc.Test(WordCount)
}
有疑问加站长微信联系(非本文作者)