A Tour of Go Exercise:Maps i 44
Go 语言练习 A Tour of Go
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.
1 package main 2 3 import ( 4 "tour/wc" 5 "strings" 6 ) 7 8 func WordCount(s string) map[string]int { 9 splitStrings := strings.Fields(s) //strings.Fields 10 vWordCount := make(map[string]int) 11 12 for _, word := range splitStrings{ 13 vWordCount[word]++ 14 } 15 16 return vWordCount 17 } 18 19 func main() { 20 wc.Test(WordCount) 21 }
浙公网安备 33010602011771号