go-learning/tour_of_go/l4/exercise-maps.go
2025-08-30 19:14:01 +01:00

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)
}