Suppose, I have an array
array1 := [5]int {
1,2,3,4,5,
}
And I need to increase this array size.
go
so that I can add additional element?If I have another array
array2 := [5]int {
6,7,8,9,10,
}
array2
with array1
?Then array1
will print [1,10]
for i:=0; i<len(array1); i++ {
fmt.Print(array1[i], "," )
}
Output:
1,2,3,4,5,6,7,8,9,10
The only way to 'resize' an array is to make a new one. You can use slices which behaves much like an array but is resized dynamically for you. You use the append
method to add items to a slice.
slice1 := []int{1,2,3,4,5}
slice2 := []int{6,7,8,9,10}
slice1 = append(slice1, slice2...)
for v, _ := range slice1 {
fmt.Println(v)
}