25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started โ†’

Interactive Go ยท Lesson

Quest: The Inventory

You’ve returned from the dungeon with a pile of loot. Time to tally it up โ€” a classic job for a slice (the loot) and a map (the running counts).

Ranging over a slice gives you each item; incrementing a map key counts them:

for _, item := range loot {
    counts[item]++
}

Incrementing a missing key just starts it from its zero value (0), so you don’t need to initialise each entry.

Your Quest

Loop over loot, count each item into the counts map, then print how many "gold" were found.

Expected output:

3

Hint

for _, item := range loot { counts[item]++ }, then fmt.Println(counts["gold"]).

Additional Scrolls of Wisdom