25% off

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

Get started →

Interactive Go · Lesson

Maps

Sometimes a list isn’t enough — you want to look things up by name. A map stores key/value pairs:

prices := map[string]int{"sword": 100, "shield": 75}
prices["bow"] = 50           // add or update
fmt.Println(prices["sword"]) // 100

Reading a missing key returns the value type’s zero value. To tell “missing” from “zero”, use the comma-ok idiom:

qty, ok := prices["axe"]   // qty=0, ok=false

Your Quest

Add 5 "arrows" to the inventory map, then print the number of arrows.

Expected output:

5

Hint

inventory["arrows"] = 5, then fmt.Println(inventory["arrows"]).

Additional Scrolls of Wisdom