Skip to content

Instantly share code, notes, and snippets.

@satyrius
Created October 23, 2013 06:23
Show Gist options
  • Select an option

  • Save satyrius/7113420 to your computer and use it in GitHub Desktop.

Select an option

Save satyrius/7113420 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 {
res := make(map[string]int)
for _, w := range strings.Fields(s) {
res[w]++
}
return res
}
func main() {
wc.Test(WordCount)
}
@VManolas
Copy link

VManolas commented May 3, 2020

Leaving this here just for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment