Tsallaka zuwa abun ciki
Justice Tech School

Practice: Word Count

Count words with a map.

Example
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "the cat and the hat and the bat"
	counts := map[string]int{}
	for _, w := range strings.Fields(text) {
		counts[w]++
	}
	fmt.Println(counts)
}

Result

map[and:2 bat:1 cat:1 hat:1 the:3]

Exercise

Split on spaces.

strings.(text)

Quiz Test

What does fmt print for maps?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.