-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaps.go
67 lines (46 loc) · 2.04 KB
/
maps.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"github.com/benhalstead/gotraining/tutorial"
)
func main() {
tutorial.Section("Maps")
//Maps map a key to a value. Any type can be stored as a value, 'comparable' types can be used as keys
//See https://blog.golang.org/go-maps-in-action
//Maps can be declared like any other type
var sim map[string]int
//But this will be nil until you initialise it with the builtin function make
sim = make(map[string]int)
//You can then write to the map...
sim["ONE"] = 1
sim["TWO"] = 2
//...and read from it
fmt.Printf("%d %d %d", sim["ONE"], sim["TWO"], sim["THREE"])
//If you request a key that is not present in the map, you will receive the ZERO VALUE for the map's value type
//In the example above, requesting sim["THREE"] returns 0, because that is the zero value for int
// If you need to know whether a map actually contained the key you asked for, you use this pattern:
if value, contains := sim["THREE"]; contains {
fmt.Printf("Map has key. Value was %d\n", value)
} else {
fmt.Printf("Map did not contain key\n")
}
// You could replace value with _ (blank identifier) in the above example if you just wanted to check that the map
// contained the value.
//Maps can also be declared as literals - note the trailing comma after the last item - this is required
tutorial.Section("Maps literals")
lm := map[string]int{
"ONE": 1,
"TWO": 2,
}
fmt.Printf("%v\n", lm)
tutorial.Section("Iterating")
// You iterate over a map with the for range construct - both the current key and value are available to your code
for k, v := range lm {
fmt.Printf("%s = %d\n", k, v)
}
// Maps do not preserve order (e.g. when you iterate over a map, the keys aren't guaranteed to be returned in the same order in which you added
// them to the map
// You CAN modify a map, while you are iterating over it (but be sure you know what you're doing)
// Maps are not goroutine safe - behaviour if two goroutines modify a map simultaneously is undefined. Use a mutex to lock access
// https://blog.golang.org/go-maps-in-action
}