Access Elements of a Slice
Reading a value from a slice works by index: put its position number in square brackets.
Positions are counted from 0, which makes [0] the first element and [1] the second, continuing the same way through the slice.
Example
Printing indexes 0 and 2 shows the first and third values in prices:
Output: Go program and captured output
package main
import ("fmt")
func main() {
prices := []int{10, 20, 30}
fmt.Println(prices[0])
fmt.Println(prices[2])
}
Change Elements of a Slice
Assigning to an index overwrites whatever value is stored there.
Example
The third value in prices goes from 30 to 50:
Output: Go program and captured output
package main
import ("fmt")
func main() {
prices := []int{10, 20, 30}
prices[2] = 50
fmt.Println(prices[0])
fmt.Println(prices[2])
}
Append Elements To a Slice
The append() function adds new values after the last element. Assign its result back to the slice, as the syntax shows:
Example
Two new values, 20 and 21, join the end of myslice1. Its length rises from 6 to 8, and its capacity grows to make room:
Output: Go program and captured output
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))
}
Syntax
slice_name = append(slice_name, element1, element2, ...)
Append One Slice To Another Slice
append() can also add a whole slice to the end of another, as the syntax shows.
Note: Those three dots after slice2 are required. They tell Go to pass the elements of the slice one by one.
Example
The values of myslice2 come after those of myslice1, and the combined result goes into myslice3:
Output: Go program and captured output
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))
}
Syntax
slice3 = append(slice1, slice2...)
Change The Length of a Slice
An array's length is fixed, but a slice's length can change.
Example
myslice1 first covers four elements of arr1, then is re-sliced down to two. Appending four values takes it to six, which is beyond its capacity of 5, so the capacity grows as well:
Output: Go program and captured output
package main
import ("fmt")
func main() {
arr1 := [6]int{9, 10, 11, 12, 13, 14}
myslice1 := arr1[1:5]
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 = arr1[1:3]
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)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
}
Memory Efficiency
A slice keeps its whole underlying array in memory, even if you only use a small part of it.
So when the array is large and you need just a few of its elements, the better choice is to copy those few with the copy() function.
Copy them into a new slice and that slice gets its own underlying array, holding only what you need. Your program then uses less memory.
Example
numbers holds 15 values, but only the first 5 are needed. They are copied into numbersCopy, created with make(), which has a length and capacity of just 5:
Output: Go program and captured output
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))
}
Syntax
copy() takes two slices, a destination dest and a source src, and copies data from the source into the destination. Its return value tells you how many elements were copied.
copy(dest, src)