-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_maps.go
More file actions
31 lines (23 loc) · 746 Bytes
/
about_maps.go
File metadata and controls
31 lines (23 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package go_koans
func aboutMaps() {
ages := map[string]int{
"bob": 10,
"joe": 20,
"dan": 30,
}
age := ages["bob"]
assert(age == 10) // map syntax is warmly familiar
age, ok := ages["bob"]
assert(ok == true) // with a handy multiple-assignment variation
age, ok = ages["steven"]
assert(age == 0) // the zero value is used when absent
assert(ok == false) // though there are better ways to check for presence
assert(len(ages) == 3) // length is based on keys
ages["bob"] = 99
assert(ages["bob"] == 99) // values can be changed for keys
ages["steven"] = 77
assert(ages["steven"] == 77) // new ones can be added
delete(ages, "steven")
age, ok = ages["steven"]
assert(ok == false) // key/value pairs can be removed
}