Golang Word Count Exercise
A sample solution for the Golang exercise to write a function that counts the number of occurrences of each word in a sentence.
package main //This covers "Exercise: Maps" import ( "golang.org/x/tour/wc" "strings" ) func WordCount(s string) map[string]int { result := make(map[string]int) words := strings.Fields(s) for x := 0; x < len(words); x++ { if _, ok := result[words[x]]; ok { result[words[x]]++ } else { result[words[x]] = 1 } } return result } func main() { wc.Test(WordCount) }

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here