Skip to content

Instantly share code, notes, and snippets.

@Aadithya-V
Created November 16, 2022 09:23
Show Gist options
  • Save Aadithya-V/bd63761d1cb3265f764c15a73e70958f to your computer and use it in GitHub Desktop.
Save Aadithya-V/bd63761d1cb3265f764c15a73e70958f to your computer and use it in GitHub Desktop.
Go Tour Map Exercised
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
tokens := strings.Fields(s)
m := make(map[string]int)
for _, token := range tokens{
elem, ok := m[token]
if ok {
m[token] = elem + 1
}
if !ok {
m[token] = 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