Maps

Looking things up by key

A slice is great for an ordered list. But often you want to look something up by a key: the rating for a given walker ID, the price for a given duration. That's a map β€” Go's dictionary.

The type spells out both halves: map[KeyType]ValueType.

  • map[string]int β€” string keys, int values (walker ID β†’ rating).
  • map[int]int β€” int keys, int values (duration in minutes β†’ price in cents).

This is Swift's Dictionary<Key, Value> β€” same idea, terser syntax. Keys are unique; assigning to an existing key overwrites it.