Go maps
Go maps
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
func maps() {
statePopulation := make(map[string]int)
statePopulation = map[string]int{
"California": 2341232,
"Texas": 3341232,
"Florida": 4341232,
"New York": 5341232,
"Illinois": 6341232,
"Ohio": 7341232,
}
fmt.Printf("State population: %v\n\n", statePopulation)
fmt.Printf("State California: %v\n", statePopulation["California"])
fmt.Printf("State Texas: %v\n", statePopulation["Texas"])
fmt.Printf("State Florida: %v\n", statePopulation["Florida"])
fmt.Printf("State New York: %v\n", statePopulation["New York"])
fmt.Printf("State Illinois: %v\n", statePopulation["Illinois"])
fmt.Printf("State Ohio: %v\n", statePopulation["Ohio"])
statePopulation["Georgia"] = 11111
fmt.Printf("Length of map: %v\n", len(statePopulation))
fmt.Printf("State Ohio: %v\n", statePopulation["Ohio"])
delete(statePopulation, "Ohio")
fmt.Printf("State population: %v\n\n", statePopulation)
// how about if we ask about non existing key ???
newStatePopulation, ok := statePopulation["xyz"]
fmt.Printf("The \"xyz\" key is present in map: %v (%v)\n", newStatePopulation, ok)
_, okk := statePopulation["xtt"]
fmt.Printf("The \"xyz\" key is present in map: (%v)\n", okk)
}
func main() {
// arrays()
// slices()
maps()
}
This post is licensed under CC BY 4.0 by the author.
