Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
go 语言之旅,练习

练习:映射
实现 WordCount。应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	wordCount := make(map[string]int)
	words := strings.Fields(s)
	for _, value := range words {
	    wordCount[value] ++;
	}
	return wordCount
}

func main() {
	wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.