Skip to content

Instantly share code, notes, and snippets.

@Mupati
Created January 4, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mupati/9bfed912fb81f3a4cbf895ccb5141872 to your computer and use it in GitHub Desktop.
Save Mupati/9bfed912fb81f3a4cbf895ccb5141872 to your computer and use it in GitHub Desktop.
Implement word count in Go
// My solution to https://go.dev/tour/moretypes/23
package main
import (
"strings"
"golang.org/x/tour/wc"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
words := strings.Fields(s)
for _, v := range words {
count, exists := m[v]
if exists {
m[v] = count + 1
} else {
m[v] = 1
}
}
return m
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment