Go Slices: Difference between revisions
Line 7: | Line 7: | ||
=Overview= | =Overview= | ||
A slice is a descriptor for a contiguous segment of an underlying [[Go_Arrays#Overview|array]]. A slice type denotes the set of all slices of arrays of its element type. The slice was used to be referred to as a [[Go_Language#Reference_Type|reference type]], but not anymore, "reference type" terminology was removed from Go documentation. | A slice is a descriptor for a contiguous segment of an underlying [[Go_Arrays#Overview|array]] and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The slice was used to be referred to as a [[Go_Language#Reference_Type|reference type]], but not anymore, "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|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. | A slice contains a pointer to the underlying array, a length and a capacity. More details available in the [[#Structure|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. |
Revision as of 00:26, 19 August 2024
External
Internal
Overview
A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The slice was used to be referred to as a reference type, but not anymore, "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 array data structure is intrinsically shared in case of pass-by-value.