Skip to content

Instantly share code, notes, and snippets.

@youchen
Created December 12, 2017 20:25
Show Gist options
  • Save youchen/17b2a3f4649844f537d6e735b30169b8 to your computer and use it in GitHub Desktop.
Save youchen/17b2a3f4649844f537d6e735b30169b8 to your computer and use it in GitHub Desktop.
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
sArr := strings.Fields(s)
count := make(map[string]int)
for index := range sArr {
ele, ok := count[sArr[index]]
if ok {
count[sArr[index]] = ele + 1
} else {
count[sArr[index]] = 1
}
}
return count
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment