Skip to content

Instantly share code, notes, and snippets.

@kevin-miles
Created April 9, 2019 22:12
Show Gist options
  • Save kevin-miles/83d14ec52711b28157cf363bb7cd6afd to your computer and use it in GitHub Desktop.
Save kevin-miles/83d14ec52711b28157cf363bb7cd6afd to your computer and use it in GitHub Desktop.
A tour of Go - Exercise: Maps
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
elements := strings.Fields(s)
elementMap := make(map[string]int)
for i := 0; i < len(elements); i++ {
elementMap[elements[i]] = elementMap[elements[i]] + 1
}
return elementMap
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment