A Go map stores values under keys chosen for the problem, rather than integer positions. In a grocery inventory, a fruit name can lead directly to its average weight; separate, position-matched lists of names and weights are unnecessary. A map has one key type and one value type, written map[K]E. section 4.3 section 4.3.1
Look up shipping weights
fruitWeight := map[string]int{"orange": 130, "apple": 195} weight := 10*fruitWeight["apple"] + 30*fruitWeight["orange"] // 5850 The literal creates and populates the map. Assigning fruitWeight["apple"] = 200 would replace the existing value. section 4.3 section 4.3.2
A few behaviors matter when putting maps to work:
- Initialize before assigning. make(map[string]int) and map[string]int{} create usable empty maps; assigning into a nil map declared with var panics. section 4.3.1
- A missing key returns the value type’s zero value. Use value, found := m[key] when zero could also be a stored value and that distinction matters. section 4.3.2
- Use range for keys and optional values, but never rely on iteration order. A range loop can also filter entries with delete when the map is not accessed elsewhere at the same time. section 4.3.5
- Changes to a map’s contents inside a function are visible to its caller. section 4.3.6
Maps use hash tables internally: a hash function selects a bucket for a key, making distribution relevant to lookup as the map grows. For domain models, a named map type can express intent—for example, GroceryList maps ingredients to measurements, while a function combines measurements across recipes. section 4.3 section 5.2.2









































