Skip to content

Instantly share code, notes, and snippets.

@zerogvt
Created February 6, 2019 09:42
Show Gist options
  • Save zerogvt/d9fd7fd05c0094b8841e4ae76b341b79 to your computer and use it in GitHub Desktop.
Save zerogvt/d9fd7fd05c0094b8841e4ae76b341b79 to your computer and use it in GitHub Desktop.
/*
Exercise: Maps
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure.
You might find strings.Fields helpful.
https://tour.golang.org/moretypes/23
*/
package main
import (
"strings"
"golang.org/x/tour/wc"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
for _, w := range strings.Fields(s) {
m[w]++
}
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