Skip to content

Instantly share code, notes, and snippets.

@satyrius
Created October 23, 2013 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyrius/7113420 to your computer and use it in GitHub Desktop.
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)
}
@sachq
Copy link

sachq commented Sep 18, 2017

Where can i get wc package?

@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