-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
128 lines (93 loc) · 3.47 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
)
func main() {
fmt.Println("-----------------------------------------------------------------------------------")
// Arrays
var arr [5]int // Declares an array of 5 integers
arr[0] = 1 // Assigning a value to the first element
arr[1] = 2 // Assigning a value to the second element
fmt.Println(arr)
fmt.Println("-----------------------------------------------------------------------------------")
// Array initialization with values
arr2 := [5]int{10, 20, 30, 40, 50}
fmt.Println("-----------------------------------------------------------------------------------")
// Array with inferred length
arr3 := [...]string{"apple", "banana", "cherry"}
fmt.Println("-----------------------------------------------------------------------------------")
// Slices
var slice []int // Declares a slice of integers (initially nil)
slice = append(slice, 1) // Appending elements to the slice
slice = append(slice, 2, 3, 4)
// Slice initialization with values
slice2 := []string{"Go", "Python", "JavaScript"}
// Slice from an array
slice3 := arr2[1:4] // Creates a slice from arr2 with elements 20, 30, 40
// Modifying the slice modifies the underlying array
slice3[0] = 25 // it can also modify array[1]
// Slice with make (length 3, capacity 5)
slice4 := make([]int, 3, 5)
slice4[0] = 100
slice4[1] = 200
slice4[2] = 300
fmt.Println("-----------------------------------------------------------------------------------")
// Range with array
fmt.Println("Array elements:")
for i, v := range arr2 {
fmt.Printf("Index %d: %d\n", i, v)
}
fmt.Println("-----------------------------------------------------------------------------------")
// Range with arr3
fmt.Println("\narr3 elements:")
for i, v := range arr3 {
fmt.Printf("Index %d: %s\n", i, v)
}
fmt.Println("-----------------------------------------------------------------------------------")
// Range with slice
fmt.Println("\nSlice elements:")
for i, v := range slice2 {
fmt.Printf("Index %d: %s\n", i, v)
}
fmt.Println("-----------------------------------------------------------------------------------")
// Range to sum elements of an array
sum := 0
for _, v := range arr2 {
sum += v
}
fmt.Println("\nSum of array elements:", sum)
fmt.Println("-----------------------------------------------------------------------------------")
// Range to modify slice elements
for i := range slice4 {
slice4[i] *= 2
}
fmt.Println("\nModified slice4 elements:", slice4)
fmt.Println("-----------------------------------------------------------------------------------")
// Map examples
// Declare and initialize a map
m := map[string]int{
"apple": 10,
"banana": 20,
"cherry": 30,
}
// Add a new key-value pair to the map
m["date"] = 40
// Update an existing key-value pair
m["banana"] = 25
// Delete a key-value pair
delete(m, "cherry")
fmt.Println("-----------------------------------------------------------------------------------")
// Iterate over the map using range
fmt.Println("\nMap elements:")
for k, v := range m {
fmt.Printf("Key: %s, Value: %d\n", k, v)
}
fmt.Println("-----------------------------------------------------------------------------------")
// Accessing a value for a specific key
if value, ok := m["apple"]; ok {
fmt.Println("\nValue for 'apple':", value)
} else {
fmt.Println("\n'apple' not found in the map")
}
fmt.Println("-----------------------------------------------------------------------------------")
}