Go Slices: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
=Structure=
=Structure=
=Declaration and Initialization=
=Declaration and Initialization=
==Initialization with <tt>make()</tt>==
==Initialization with a Composite Literal==


=TODEPLETE=
=TODEPLETE=
{{Internal|Go_Slices_TODEPLETE|Go Slices TODPLETE}}
{{Internal|Go_Slices_TODEPLETE|Go Slices TODPLETE}}

Revision as of 17:21, 15 August 2024

External

Internal

Overview

A slice is a descriptor for a contiguous segment of an underlying array. A slice type denotes the set of all slices of arrays of its element types. The slice was used to be referred to as a reference type, but not anymore, as reference type terminology was removed from Go documentation.

A slice contains a pointer to the underlying array, a length and a capacity. More details available in the structure section. A slice, once initialized, is always associated with the underlaying array that holds its elements. The slice shares storage with its array, and other slices of the same array.

Go uses pass-by-value, so when slice arguments are passed to functions, the internal fields are copied across, including the pointer to the underlying array. Therefore, the underlying data structure is intrinsically shared even in presence of pass-by-value.

Structure

Declaration and Initialization

Initialization with make()

Initialization with a Composite Literal

TODEPLETE

Go Slices TODPLETE