go-learning/tour_of_go/l4/exercise-maps.go

27 lines
371 B
Go
Raw Normal View History

2025-08-30 18:14:01 +00:00
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)
}