go语言Exercise: Maps
2012-05-09 11:02
183 查看
Implement
The
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)
}
WordCount. It should return a map of the counts of each “word” in the string
s.
The
wc.Testfunction 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)
}
相关文章推荐
- go语言Exercise: Slices
- go语言Exercise: Fibonacci closure
- go语言Advanced Exercise: Complex cube roots
- go语言Exercise: Errors
- Go语言入门教程之Arrays、Slices、Maps、Range操作简明总结
- go语言Exercise: Images
- go语言Exercise: Rot13 Reader
- Go 语言实现exercise--Loops and Functions
- go语言Exercise: Equivalent Binary Trees
- A Tour of Go : Exercise: Maps
- A Tour of Go Exercise: Maps
- Go语言学习(二)常量
- Go语言反射之类型反射
- GO语言延迟函数defer用法分析
- 深入解析快速排序算法的原理及其Go语言版实现
- go语言快速入门:流程控制(7)
- Go语言中的map用法
- go语言快速入门:IPC之Socket(9)
- go语言自己实现Set
- go语言中的main函数和init函数