Go Access, Change, Append and Copy Slices
Access Elements of a Slice
You can access a specific slice element by referring to the index number.
In Go, indexes start at 0. That means that [0] is the first element, [1] is the second element, etc.
Example
This example shows how to access the first and third elements in the prices slice:
package main
import ("fmt")
func main() {
prices := []int{10,20,30}
fmt.Println(prices[0])
fmt.Println(prices[2])
}
Result:
10
30
Change Elements of a Slice
You can also change a specific slice element by referring to the index number.
Example
This example shows how to change the third element in the prices slice:
package main
import ("fmt")
func main() {
prices := []int{10,20,30}
prices[2] = 50
fmt.Println(prices[0])
fmt.Println(prices[2])
}
Result:
10
50
Append Elements To a Slice
You can append elements to the end of a slice using the append()
function:
Syntax
slice_name = append(slice_name, element1, element2, ...)
Example
This example shows how to append elements to the end of a slice:
package main
import ("fmt")
func main() {
myslice1 := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("myslice1 =
%v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 =
append(myslice1, 20, 21)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity =
%d\n", cap(myslice1))
}
Result:
myslice1 = [1 2 3 4 5 6]
length = 6
capacity = 6
myslice1 = [1 2 3
4 5 6 20 21]
length = 8
capacity = 12
Append One Slice To Another Slice
To append all the elements of one slice to another slice, use the append()
function:
Syntax
slice3 = append(slice1, slice2...)
Note: The '...' after slice2 is necessary when appending the elements of one slice to another.
Example
This example shows how to append one slice to another slice:
package main
import ("fmt")
func main() {
myslice1 := []int{1,2,3}
myslice2 := []int{4,5,6}
myslice3 := append(myslice1, myslice2...)
fmt.Printf("myslice3=%v\n",
myslice3)
fmt.Printf("length=%d\n", len(myslice3))
fmt.Printf("capacity=%d\n", cap(myslice3))
}
Result:
myslice3=[1 2 3 4 5 6]
length=6
capacity=6
Change The Length of a Slice
Unlike arrays, it is possible to change the length of a slice.
Example
This example shows how to change the length of a slice:
package main
import ("fmt")
func main() {
arr1 := [6]int{9, 10, 11, 12, 13, 14} // An array
myslice1
:= arr1[1:5] // Slice array
fmt.Printf("myslice1 = %v\n",
myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 =
arr1[1:3] // Change length by re-slicing the array
fmt.Printf("myslice1
= %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 =
append(myslice1, 20, 21, 22, 23) // Change length by appending items
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length =
%d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
}
Result:
myslice1 = [10 11 12 13]
length = 4
capacity = 5
myslice1 = [10 11]
length = 2
capacity = 5
myslice1 = [10 11 20 21 22 23]
length = 6
capacity = 10
Memory Efficiency
When using slices, Go loads all the underlying elements into the memory.
If the array is large and you need only a few elements, it is better to copy those elements using the copy()
function.
The copy()
function creates a new underlying array with only the required elements for the slice. This will reduce the memory used for the program.
Syntax
copy(dest, src)
The copy()
function takes in two slices
dest and src, and copies data from src to dest.
It returns the number of elements copied.
Example
This example shows how to use the copy()
function:
package main
import ("fmt")
func main() {
numbers := []int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
// Original slice
fmt.Printf("numbers = %v\n", numbers)
fmt.Printf("length = %d\n", len(numbers))
fmt.Printf("capacity =
%d\n", cap(numbers))
// Create copy with only needed numbers
neededNumbers := numbers[:len(numbers)-10]
numbersCopy := make([]int,
len(neededNumbers))
copy(numbersCopy, neededNumbers)
fmt.Printf("numbersCopy = %v\n", numbersCopy)
fmt.Printf("length =
%d\n", len(numbersCopy))
fmt.Printf("capacity = %d\n", cap(numbersCopy))
}
Result:
// Original slice
numbers = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
length = 15
capacity =
15
// New slice
numbersCopy = [1 2 3 4 5]
length = 5
capacity = 5
The capacity of the new slice is now less than the capacity of the original slice because the new underlying array is smaller.