Skip to content

Instantly share code, notes, and snippets.

@zonggen
Last active June 17, 2018 22:15
Show Gist options
  • Save zonggen/709f24340d4d9e6f4ba253772a9308de to your computer and use it in GitHub Desktop.
Save zonggen/709f24340d4d9e6f4ba253772a9308de to your computer and use it in GitHub Desktop.
A Tour of Go - Maps Exercise Answer
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
m := make (map[string]int)
words := strings.Fields(s)
for _, key := range words{
_, ok := m [key]
if ok{
m [key] ++
} else {
m [key] = 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