Maps in Go
Key-value storage and the comma-ok idiom.
func mapBasics() (int, bool, int) {
scores := map[string]int{"ada": 95, "grace": 88}
scores["kay"] = 92
ada := scores["ada"]
value, ok := scores["alan"]
delete(scores, "grace")
total := 0
for _, score := range scores {
total += score
}
return ada, ok, total + value
}
How it works
- A literal or assignment populates the map.
value, ok := m[k]reports whether the key existed.deleteremoves;rangeiterates.
Keywords and builtins used here
booldeleteforfuncintmaprangereturnstring
The run, in numbers
- Lines
- 14
- Characters to type
- 269
- Tokens
- 78
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 52 seconds.
Step 4 of 9 in Arrays, slices & maps, step 19 of 30 in Language basics.