@@ -95,6 +95,23 @@ func learnDynamicArrays() {
95
95
96
96
printSlice (dummySlice )
97
97
98
+ // Creating a slice with make
99
+
100
+ fmt .Println ("Creating slices with make, lets see the length and capacity" )
101
+
102
+ // assigns a length of 5
103
+ newSlice := make ([]int , 5 )
104
+
105
+ printSlice (newSlice )
106
+
107
+ // assigns a capacity of 5
108
+ newSlice = make ([]int , 0 , 5 )
109
+
110
+ newSlice = newSlice [:cap (newSlice )]
111
+ newSlice = newSlice [0 :3 ]
112
+
113
+ printSlice (newSlice )
114
+
98
115
}
99
116
100
117
func createSlicesOfSlices () {
@@ -123,7 +140,30 @@ func createSlicesOfSlices() {
123
140
}
124
141
}
125
142
126
- func printSlice [T []int ](val T ) {
143
+ func appendThingsToSlices () {
144
+ fmt .Println ("This is appending someting to slices" )
145
+
146
+ someArray := [5 ]string {
147
+ "Aniket" ,
148
+ "Animesh" ,
149
+ "Ankush" ,
150
+ "Ankit" ,
151
+ "Amaan" ,
152
+ }
153
+
154
+ someSlice := make ([]string , 0 )
155
+
156
+ for i := 0 ; i < len (someArray ); i ++ {
157
+ someSlice = append (someSlice , someArray [i ])
158
+ }
159
+
160
+ someSlice = append (someSlice , "Adding name: Amitava" )
161
+ fmt .Println ("Slice after appending" )
162
+ fmt .Println (someSlice )
163
+ printSlice (someSlice )
164
+ }
165
+
166
+ func printSlice [T []int | []string ](val T ) {
127
167
fmt .Printf ("\n len = %v, cap = %v \n " , len (val ), cap (val ))
128
168
}
129
169
@@ -136,5 +176,11 @@ func main() {
136
176
137
177
learnDynamicArrays ()
138
178
179
+ // create 2D arrays
180
+
139
181
createSlicesOfSlices ()
182
+
183
+ // appendThingsToSlices
184
+
185
+ appendThingsToSlices ()
140
186
}
0 commit comments