Go Slice Expressions: Difference between revisions

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


The new slice created by a slice expression shares the underlying array with the original slice.
A slice expression applied to a slice creates a new slice that shares the underlying array with the original slice.
 
<syntaxhighlight lang='go'>
ss := make([]int, 10) // declare a slice with length and capacity 10, initialized with 0
ss2 := ss[3:6]
</syntaxhighlight>
The slice expression creates a different slice that provides access to the elements with indexes 3, 4, 5 of the underlying array. Both slices <code>ss</code> and <code>ss2</code> provide access to those elements and changes operated via one slice are visible to the other.

Revision as of 02:25, 19 August 2024

Internal

TODO

Overview

A slice expression applied to a slice creates a new slice that shares the underlying array with the original slice.

ss := make([]int, 10) // declare a slice with length and capacity 10, initialized with 0
ss2 := ss[3:6]

The slice expression creates a different slice that provides access to the elements with indexes 3, 4, 5 of the underlying array. Both slices ss and ss2 provide access to those elements and changes operated via one slice are visible to the other.