Taking part of a slice

Sometimes you need only part of a slice: the first three scores, or everything after the first line. A slice expression gives you that part. You write s[low:high], and you get the elements from index low up to, but not including, index high.

package main

import "fmt"

func main() {
	nums := []int{10, 20, 30, 40, 50}
	part := nums[1:4]
	fmt.Println(part)
	fmt.Println(len(part))
}

Output:

[20 30 40]
3

nums[1:4] starts at index 1 and stops before index 4, so it holds the elements at indexes 1, 2 and 3:

 index:    0    1    2    3    4
         [ 10 | 20 | 30 | 40 | 50 ]
                ^^^^^^^^^^^^
                  nums[1:4]

Not including the end has a good side: the length of the result is always high - low. Here that is 4 - 1 = 3.

Leaving out a bound

You can leave out either number. Without low, the part starts at index 0. Without high, it goes to the end:

nums := []int{10, 20, 30, 40, 50}
fmt.Println(nums[:2])
fmt.Println(nums[3:])
fmt.Println(nums[1:])

Output:

[10 20]
[40 50]
[20 30 40 50]

nums[:2] is the first 2 elements, and nums[3:] is everything from index 3 on. nums[1:], everything except the first element, is very common in loops and recursion. When low and high are equal, as in nums[2:2], the result is an empty slice.

The part shares the underlying array

Taking a part does not copy any elements. The new slice refers to the same underlying array as the original, so a change through one shows in the other:

nums := []int{10, 20, 30, 40, 50}
part := nums[1:4]
part[0] = 99
fmt.Println(part)
fmt.Println(nums)

Output:

[99 30 40]
[10 99 30 40 50]

part[0] and nums[1] are the same element. This makes slicing fast, even for a very long slice. But it surprises many learners, who change the part and expect the original to stay the same.

append can change the original too

A part also shares the room after its end. cap shows how far that room goes:

nums := []int{10, 20, 30, 40, 50}
part := nums[1:3]
fmt.Println(part, len(part), cap(part))
part = append(part, 99)
fmt.Println(part)
fmt.Println(nums)

Output:

[20 30] 2 4
[20 30 99]
[10 20 30 99 50]

part has 2 elements but a capacity of 4, because the underlying array goes on to the end of nums. So append did not need a new array. It wrote 99 into the next slot, which is nums[3], and the 40 is gone. To append to a part without touching the original, make a real copy first.

Making a real copy

When you need a separate copy, make a new slice with the right length and use the built-in function copy. It copies the elements of the second slice into the first:

nums := []int{10, 20, 30, 40, 50}
part := make([]int, 3)
copy(part, nums[1:4])
part[0] = 99
fmt.Println(part)
fmt.Println(nums)

Output:

[99 30 40]
[10 20 30 40 50]

Now part has its own underlying array, and nums did not change. copy only fills the elements that already exist in the new slice, so it must get its length from make first. An empty slice would receive nothing.

Bounds that do not exist

A bound past the end stops the program with a panic, like a bad index does:

nums := []int{10, 20, 30, 40, 50}
end := 6
fmt.Println(nums[1:end])

Output:

panic: runtime error: slice bounds out of range [:6] with capacity 5

The message talks about capacity, which you met in the last lesson. For a slice like nums, the length and the capacity are both 5. Keep both bounds between 0 and len(nums), and low no bigger than high.

In the exercise, read a slice and two positions, and print the part of the slice between them and its length.