Skip to content

Instantly share code, notes, and snippets.

@bilsalak
Created December 2, 2018 01:09
Show Gist options
  • Save bilsalak/51d0f26bb9bb02f307b8583cabcfef28 to your computer and use it in GitHub Desktop.
Save bilsalak/51d0f26bb9bb02f307b8583cabcfef28 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 (
"strings"
"golang.org/x/tour/wc"
)
func WordCount(s string) map[string]int {
word_count_map := make(map[string]int)
words := strings.Fields(s)
for _, word := range words {
element, _ := word_count_map[word]
word_count := element + 1
word_count_map[word] = word_count
}
return word_count_map
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment