Skip to content

Instantly share code, notes, and snippets.

@nowk
Created July 8, 2014 01:46
Show Gist options
  • Save nowk/9a5619824dd6d8cd0c71 to your computer and use it in GitHub Desktop.
Save nowk/9a5619824dd6d8cd0c71 to your computer and use it in GitHub Desktop.
Wordcount - Go Guide #43
package wordcount
import "strings"
func WordCount(s string) map[string]int {
var (
arrOfWords []string
wordMap map[string]int
)
wordMap = make(map[string]int)
arrOfWords = strings.Fields(s)
for _, v := range arrOfWords {
count := wordMap[v]
wordMap[v] = count + 1
}
return wordMap
}
package wordcount
import (
"testing"
"github.com/bmizerany/assert"
)
func TestWordCount(t *testing.T) {
w := WordCount("foo bar bar foo foo foo")
var s = map[string]int{
"foo": 4,
"bar": 2,
}
assert.Equal(t, w, s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment