27 lines
371 B
Go
27 lines
371 B
Go
package main
|
|
|
|
import (
|
|
"golang.org/x/tour/wc"
|
|
"strings"
|
|
)
|
|
|
|
func WordCount(s string) map[string]int {
|
|
result := map[string]int{}
|
|
fields := strings.Fields(s)
|
|
for i := range fields {
|
|
word := fields[i]
|
|
_, exists := result[word]
|
|
if exists == true {
|
|
result[word] += 1
|
|
} else {
|
|
result[word] = 1
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func main() {
|
|
wc.Test(WordCount)
|
|
}
|
|
|