Skip to content

Instantly share code, notes, and snippets.

@neilchaudhuri
Created March 14, 2019 23:48
Show Gist options
  • Save neilchaudhuri/357962559b337e02be3b21d2a6a9ceb9 to your computer and use it in GitHub Desktop.
Save neilchaudhuri/357962559b337e02be3b21d2a6a9ceb9 to your computer and use it in GitHub Desktop.
Example of idiomatic Go for counting words in a collection
package main
import "fmt"
import "strings"
func main() {
dictionary := make(map[string]int)
words := []string{"Fear", "of", "a", "name", "only", "increases", "fear", "of", "the", "thing", "itself"}
for _, word := range words {
word = strings.ToLower(word)
if count, ok := dictionary[word]; ok {
dictionary[word] = count + 1
} else {
dictionary[word] = 1
}
}
fmt.Printf("The counts are %v\n", dictionary)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment