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.

exercise-maps.go

  1. package main
  2. import (
  3. "golang.org/x/tour/wc"
  4. )
  5. func WordCount(s string) map[string]int {
  6. return map[string]int{"x": 1}
  7. }
  8. func main() {
  9. wc.Test(WordCount)
  10. }