您的位置:首页 > 编程语言 > Go语言

go语言Exercise: Maps

2012-05-09 11:02 183 查看
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)

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: