Skip to content

Instantly share code, notes, and snippets.

@fr-xiaoli
Created November 17, 2018 01:31
Show Gist options
  • Select an option

  • Save fr-xiaoli/0d6450a356292fe7ba9b492aa1438e48 to your computer and use it in GitHub Desktop.

Select an option

Save fr-xiaoli/0d6450a356292fe7ba9b492aa1438e48 to your computer and use it in GitHub Desktop.
A Tour of Go - Basics - Exercise: Maps
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) (m map[string]int) {
m = make(map[string]int)
ws := strings.Fields(s)
for _, w := range ws {
_, ok := m[w]
if ok {
m[w] += 1
} else {
m[w] = 1
}
}
return
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment