Skip to content

Instantly share code, notes, and snippets.

@sighmin
Created February 23, 2014 17:27
Show Gist options
  • Select an option

  • Save sighmin/9174399 to your computer and use it in GitHub Desktop.

Select an option

Save sighmin/9174399 to your computer and use it in GitHub Desktop.
Go tour "WordCount" method: http://tour.golang.org
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
tokens := strings.Fields(s)
hashmap := make(map[string] int)
var acc int
var exists bool
for i := 0; i < len(tokens); i++ {
elem := tokens[i]
acc, exists = hashmap[elem]
if exists {
hashmap[elem] = acc + 1
} else {
hashmap[elem] = 1
}
}
return hashmap
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment