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

Related Content

Empty structs

We finally we have enough knowledge for the EBNF format not to seem completely foreign, so let’s jump back and take a look at that, with the examples provided in the spec… Struct types … StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit . // An empty struct.

Struct tags

Struct types … A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored. struct { x, y float64 "" // an empty tag string is like an absent tag name string "any string is permitted as a tag" _ [4]byte "ceci n'est pas un champ de structure" } // A struct corresponding to a TimeStamp protocol buffer.

Struct method promotion

Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted. Struct types … Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows: If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.