Created
March 14, 2019 23:48
-
-
Save neilchaudhuri/357962559b337e02be3b21d2a6a9ceb9 to your computer and use it in GitHub Desktop.
Example of idiomatic Go for counting words in a collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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