Created
November 1, 2016 20:08
-
-
Save alexbosworth/64356f32781369b15fda0072e17c3619 to your computer and use it in GitHub Desktop.
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 { | |
countForWord := make(map[string]int) | |
for _, word := range strings.Fields(s) { | |
var currentCount int | |
if count, set := countForWord[word]; set { | |
currentCount = count | |
} else { | |
currentCount = 0 | |
} | |
countForWord[word] = currentCount + 1 | |
} | |
return countForWord | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment