By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Slices are numbered: expenses[0], expenses[1]. Often you want to look something up by a name. Penny wants totals per category: food, travel, rent. A map stores key-value pairs, so you look up a key such as "food" and get a value such as 1250.
The key type and the value type are fixed when you create the map. map[string]int means string keys mapping to int values. Read it as "a map from string to int". Every key in one map has the same type, and so does every value.
A map literal uses map[K]V{...} with key-value pairs. Reading uses the same square brackets as a slice, but with the key inside instead of an index.
package main
import "fmt"
func main() {
totals := map[string]int{
"food": 1250,
"travel": 4200,
}
fmt.Println("food:", totals["food"])
fmt.Println("rent:", totals["rent"])
fmt.Println("categories:", len(totals))
}totals["food"] returns 1250. totals["rent"] is not an error: a map returns the zero value for a missing key, so you get 0. That is convenient and dangerous at the same time, because the value alone cannot tell you whether rent is truly zero or was never tracked.
The comma-ok form tells you whether the key exists: value, ok := totals["rent"]. If the key is present, ok is true. If it is missing, ok is false and value is the zero value. Use it whenever 0 is a meaningful value.
package main
import "fmt"
func main() {
totals := map[string]int{"food": 1250, "travel": 4200}
rent, ok := totals["rent"]
fmt.Println("rent:", rent, "found:", ok)
totals["rent"] = 300
rent, ok = totals["rent"]
fmt.Println("rent:", rent, "found:", ok)
delete(totals, "travel")
_, ok = totals["travel"]
fmt.Println("travel found:", ok)
}The two-value shape should look familiar from step 18: a value and a boolean that says whether to trust it. Go reuses that pattern deliberately, and you will meet it again with type assertions in section 6.
Assignment adds or updates: totals["rent"] = 300 creates the key if it is new and replaces the value if it already exists. delete(totals, "travel") removes a key; deleting a key that is not there does nothing and is not an error. len(totals) counts the pairs that currently exist.
Notice that no function call is needed to add an entry. A map behaves like a slice for assignment, with a key instead of an index, and you never have to worry about the map being full.
When you range over a map you get key and value, but the order is not the insertion order and it is not sorted. Go randomises it on purpose so that no program accidentally depends on an internal order. The order can differ between two runs of the same program.
If you need a stable order, collect the keys into a slice, sort the slice, then range over the slice. That pattern combines everything in this section: a map for lookup, a slice for order, append to build the slice, and sort to put it in order.
package main
import (
"fmt"
"sort"
)
func main() {
totals := map[string]int{"food": 1250, "travel": 4200, "rent": 300}
keys := make([]string, 0, len(totals))
for category := range totals {
keys = append(keys, category)
}
sort.Strings(keys)
for _, category := range keys {
fmt.Println(category, totals[category])
}
}Notice the output is alphabetical only because the code sorted the keys. Removing the sort would still compile, but the lines would appear in an unspecified order - a classic source of a test that passes on your machine and fails on someone else's.
A map is a hash table. Hashing turns the key into a number, and the number selects a bucket where entries are stored. The diagram follows one lookup from key to value.
Because the hash goes straight to a bucket, the cost of a lookup stays roughly flat as the map grows. That is what "constant time" means in the title: one lookup among a thousand entries costs about the same as one among a million. A slice would have to scan every item.
Count categories in a slice. Start with categories := []string{"food", "travel", "food", "rent", "food"} and counts := map[string]int{}. For each category, run counts[category]++: reading a missing key gives 0, and the increment writes the new value back. Then print the counts in sorted key order. Expected output: food 3, rent 1, travel 1.
Key takeaways
A map stores key-value pairs: map[string]int maps string keys to int values.
A missing key returns the zero value; value, ok := m[key] tells you whether the key exists.
Assigning adds or updates an entry, delete(m, key) removes one, and len(m) counts pairs.
Map range order is deliberately random; sort the keys when output must be stable.
Hashing sends each key straight to a bucket, so lookups stay roughly constant time.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.