Implementation of the Map exercise in “A Tour of Go”

Source: http://www.digitalthirdcoast.net/blog/create-successful-keyword-mapping-strategy

When you begin to learn the trending Go lang you may have encounter with the map exercise to implement the WordCount. Here is how I implemented it.

package main

import (
"golang.org/x/tour/wc"
"strings"
)

func WordCount(s string) map[string]int {

var stringArray []string = strings.Fields(s)
wordMap := make(map[string]int)
for _, word := range stringArray {
count, ok := wordMap[word]
if ok {
wordMap[word] = count + 1
} else {
wordMap[word] = 1
}
}
return wordMap
}

func main() {
wc.Test(WordCount)
}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade