Created
April 2, 2014 04:08
-
-
Save raypereda/9927778 to your computer and use it in GitHub Desktop.
count the words in a string
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 ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
"fmt" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
fmt.Println(words) | |
count := make(map[string]int) | |
for _, word := range words { | |
v, present := count[word] | |
if present { | |
count[word] = v + 1 | |
} else { | |
count[word] = 1 | |
} | |
} | |
return count | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment