Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created August 22, 2018 13:05
Show Gist options
  • Save codecakes/98626654c32c0ff2f06caa64d2926162 to your computer and use it in GitHub Desktop.
Save codecakes/98626654c32c0ff2f06caa64d2926162 to your computer and use it in GitHub Desktop.
count words in go
package main
import (
"strings"
"golang.org/x/tour/wc"
)
func WordCount(s string) map[string]int {
result := make(map[string]int)
present := false
for _, word := range strings.Fields(s) {
if _, present = result[word]; present {
result[word] += 1
} else {
result[word] = 1
}
}
return result
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment