Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created March 23, 2014 07:04
Show Gist options
  • Save dz1984/9719747 to your computer and use it in GitHub Desktop.
Save dz1984/9719747 to your computer and use it in GitHub Desktop.
"A Tour of Go"
/*
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.
*/
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
result := make(map[string]int)
for _,w := range strings.Fields(s){
nums,_ := result[w]
result[w] = nums+1
}
return result
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment