Created
July 14, 2013 04:52
-
-
Save MattSurabian/5993259 to your computer and use it in GitHub Desktop.
My implementation of WordCount in Go. Part of the Go tour: http://tour.golang.org/#40
This file contains 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" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
wordCountMap := make(map[string]int) | |
for _,word := range words{ | |
wordCountMap[word]++ | |
} | |
return wordCountMap | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
wc := make(map[string]int)
for _, word := range strings.Split(s, " ") {
if _, exist := wc[word]; !exist {
wc[word] = 0
}
wc[word] += 1
}
return wc
}
func main() {
wc.Test(WordCount)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's a very nice solution, i came up with a rather ugly one.