Created
June 30, 2017 09:04
-
-
Save tossmilestone/0d88be02be6d1d5ecf1b80861483380d to your computer and use it in GitHub Desktop.
Words count in go tour
This file contains hidden or 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 ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
counts := make(map[string]int) | |
for _, w := range words { | |
_, ok := counts[w] | |
if ok { | |
counts[w] += 1 | |
} else { | |
counts[w] = 1 | |
} | |
} | |
return counts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment