Skip to content

Instantly share code, notes, and snippets.

@ifreesec
Created July 17, 2018 06:50
Show Gist options
  • Save ifreesec/ad2c799ad54bd29e8eb5fc0fa9096628 to your computer and use it in GitHub Desktop.
Save ifreesec/ad2c799ad54bd29e8eb5fc0fa9096628 to your computer and use it in GitHub Desktop.
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