Skip to content

Instantly share code, notes, and snippets.

@yixiaoyx
Last active May 26, 2019 02:33
Show Gist options
  • Save yixiaoyx/dca24500ef3bd524225dc2e2e387d9c4 to your computer and use it in GitHub Desktop.
Save yixiaoyx/dca24500ef3bd524225dc2e2e387d9c4 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise
// count occurrences of words in a sentence
package main
import (
"golang.org/x/tour/wc"
"strings"
)
// my solution here
func WordCount(s string) map[string]int {
m := make(map[string]int)
f := strings.Fields(s)
for _, w := range f {
m[w] += 1
}
return m
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment