Go Slices: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:
=Overview=
=Overview=


A ''slice'' is a [[Go_Concepts_-_The_Type_System#Reference_Types|reference type]] that implements a ''dynamic array''. Slices are indexable, and they have a variable length. They are always associated with an underlying array, and the slice length cannot be longer than the underlying array - but it can be shorter. The [[Go_Slices#Slice_Capacity|slice's capacity]] is equal to the length on the underlying array.
A ''slice'' is a [[Go_Concepts_-_The_Type_System#Reference_Types|reference type]] that implements a ''dynamic array''. Slices are indexable, and they have a variable length. They are always associated with an underlying array, and the slice length cannot be longer than the underlying array - but it can be shorter. The [[Go_Slices#Slice_Capacity|slice's capacity]] is equal to the length on the underlying array. The slice is expanded beyond the capacity of the underlying array by creating a new, longer underlying array.


Lexically, a slice type is a [[Go_Concepts_-_The_Type_System#Reference_Types|reference type]]. The slice instances must be initialized before attempting to use into them, either by using the <tt>[[#make.28.29|make()]]</tt> function or a slice literal, otherwise you will get a runtime error, because a [[Go_Concepts_-_The_Type_System#Zero_Value|zero value]] for a slice is <tt>nil</tt>.
Lexically, a slice type is a [[Go_Concepts_-_The_Type_System#Reference_Types|reference type]]. The slice instances must be initialized before attempting to use into them, either by using the <tt>[[#make.28.29|make()]]</tt> function or a slice literal, otherwise you will get a runtime error, because a [[Go_Concepts_-_The_Type_System#Zero_Value|zero value]] for a slice is <tt>nil</tt>.

Revision as of 06:22, 6 April 2016

External

Internal

Overview

A slice is a reference type that implements a dynamic array. Slices are indexable, and they have a variable length. They are always associated with an underlying array, and the slice length cannot be longer than the underlying array - but it can be shorter. The slice's capacity is equal to the length on the underlying array. The slice is expanded beyond the capacity of the underlying array by creating a new, longer underlying array.

Lexically, a slice type is a reference type. The slice instances must be initialized before attempting to use into them, either by using the make() function or a slice literal, otherwise you will get a runtime error, because a zero value for a slice is nil.

Implementation Details

TODO

Accessing the Slice's Underlying Array

TODO

Declaration

Long Declaration

A slice declaration is similar to an array's except the length is not specified. The slice is created with a zero length if no literal is specified.

var s []int

A slice declaration with a literal:

var s []int = []int {1, 2, 3}

Short Declaration

A short declaration using a slice literal:

s := []int{1, 2, 3}

A short declaration can be performed using make(). make() takes the length and the capacity of the slice.

The slices can also be created with the "slice" expression:

<array_identifier>[<low>:<high>]

Example:

var a [5]int
s := a[0:2]

The "low" index is the index where to start the slice and "high" is the index where to end the slice - but not index itself.

Special forms of the "slice" expression:

  • a[i:] is equivalent with a[i:len(a)].
  • a[:i] is equivalent with a[0:i]
  • a[:] is equivalent with a[0:len(a)]

Slice Operators and Functions

Slice Length

len() returns the length of the slice.

Slice Capacity

cap() returns the length of the underlying array.

make()

The make() function creates the slice and the underlying array.

<slice_identifier> := make([]<slice_element_type>, <slice_length>, <capacity>)
s := make([]int, 5)
s1 := make([]int, 5, 10)

Note that make() returns the slice instance, not a pointer.

append()

append() adds element at the end of the slice, increasing its length. The addition is operated by writing the values in the corresponding positions in the underlying array, unless the array is not long enough. In this case, a new array long enough to accommodate the new elements is created and the original array values are copied across.

The function always creates a new slice instance, don't expect that a lateral effect will change your input slice

Example:

bs := make([]byte, 0) // the length of bs is 0

bs2 := append(bs, byte(1)) // the length of bs stays 0 
                           // but the length of bs2 (a new slice) is 1

copy()

copy(dest_slice, source_slice)

The function copies all the source elements over the corresponding elements in the destination, overwriting the destination. If the lengths of the slices are not the same, the shortest one will be used.

Passing a Slice's Elements to a Variadic Function

Variadic Functions