Skip to content

Instantly share code, notes, and snippets.

@raypereda
Created April 2, 2014 04:08
Show Gist options
  • Save raypereda/9927778 to your computer and use it in GitHub Desktop.
Save raypereda/9927778 to your computer and use it in GitHub Desktop.
count the words in a string
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