-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (44 loc) · 1.26 KB
/
main.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
package main
import "fmt"
func main() {
// a. Declaration
// i. Using var keyword
var colors1 = map[string]string{}
// ii. Using make built-in function
colors2 := make(map[string]string)
// b. Initialization
colors3 := map[string]string{
"red": "#ff0000",
"blue": "#0000ff",
"green": "#00ff00",
}
// c. Put, get and delete elements
colors2["red"] = "#ff0000"
colors2["blue"] = "#0000ff"
fmt.Println("After adding elements:", colors2)
redValue := colors2["red"]
fmt.Println("Red value:", redValue)
delete(colors2, "blue")
fmt.Println("After deleting blue:", colors2)
// d. Two returns of get element
yellowValue, exists := colors3["yellow"]
if exists {
fmt.Println("Yellow value:", yellowValue)
} else {
fmt.Println("Yellow doesn't exist in the map")
}
// e. Iterations
// i. Using range to iterate over keys
fmt.Println("Iterating over keys:")
for color := range colors3 {
fmt.Println(color, ":", colors3[color])
}
// ii. By range clause (unchanged)
fmt.Println("Iterating over key-value pairs:")
for color, hex := range colors3 {
fmt.Println("Color:", color, "Hex:", hex)
}
fmt.Println("colors1 (declared with var):", colors1)
fmt.Println("colors2 (created with make):", colors2)
fmt.Println("colors3 (initialized):", colors3)
}