Skip to content

Instantly share code, notes, and snippets.

@jcreager
Created November 7, 2017 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreager/61cd3a666e730b3689f240f8f2901dec to your computer and use it in GitHub Desktop.
Save jcreager/61cd3a666e730b3689f240f8f2901dec to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Maps
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
var count map[string]int
count = make(map[string]int)
for i := 0; i < len(words); i++ {
v, ok := count[words[i]]
if ok != true {
count[words[i]] = 1
} else {
count[words[i]] = v + 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