Slice storage

March 6, 2023

Slice types

The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

A slice, once initialized, is always associated with an underlying array that holds its elements. A slice therefore shares storage with its array and with other slices of the same array; by contrast, distinct arrays always represent distinct storage.

Last week we saw that a slice is effectively a view into an array.

These two paragraphs expand on that concept, pointing out a couple of important implications this has.

  1. There’s no relationship between the index of an element in a slice, and the index of the same element in the underlying array. Consider:

    	// Initialize a as an array of 10 integers
    	var a = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    	// Initialize s as a slice "view" of a, of indexes 0, 1, and 2
    	var s = a[3:6]
    	fmt.Println(a, s) // Prints: [0 1 2 3 4 5 6 7 8 9] [3 4 5]
    

    s[0] is the same as a[3], and a[0] is not present in the slice s at all.

  2. Because a slice and array use the same backing memory, modifying one will modify the other as well:

    	var a = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    	var s = a[3:6]
    	s[0] = 99
    	fmt.Println(a, s) // Prints [0 1 2 99 4 5 6 7 8 9] [99 4 5]
    

    Notice that both the slice and the underlying array are modified.

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Clear

Clear The built-in function clear takes an argument of map, slice, or type parameter type, and deletes or zeroes out all elements [[Go 1.21(https://go.dev/ref/spec#Go_1.21)]]. Call Argument type Result clear(m) map[K]T deletes all entries, resulting in an empty map (len(m) == 0) clear(s) []T sets all elements up to the length of s to the zero value of T clear(t) type parameter see below clear is a relatively recent addition to Go, added just over a year ago.


Conversions from slice to array or array pointer

Conversions from slice to array or array pointer … Converting a slice to an array yields an array containing the elements of the underlying array of the slice. Similarly, converting a slice to an array pointer yields a pointer to the underlying array of the slice. In both cases, if the length of the slice is less than the length of the array, a run-time panic occurs. s := make([]byte, 2, 4) a0 := [0]byte(s) a1 := [1]byte(s[1:]) // a1[0] == s[1] a2 := [2]byte(s) // a2[0] == s[0] a4 := [4]byte(s) // panics: len([4]byte) > len(s) s0 := (*[0]byte)(s) // s0 !


Simple slice expression shortcuts

Simple slice expressions, which we started with yesterday, offer us two shortcuts: Simple slice expressions … For convenience, any of the indices may be omitted. A missing low index defaults to zero; a missing high index defaults to the length of the sliced operand: a[2:] // same as a[2 : len(a)] a[:3] // same as a[0 : 3] a[:] // same as a[0 : len(a)] This should be pretty easy to understand.

Get daily content like this in your inbox!

Subscribe