Go Slices: Difference between revisions
Jump to navigation
Jump to search
(→make()) |
|||
Line 6: | Line 6: | ||
=Overview= | =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. | 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. | ||
<font color=red>'''TODO''' Deep difference between slice and array (memory model, etc.)</font> | <font color=red>'''TODO''' Deep difference between slice and array (memory model, etc.)</font> |
Revision as of 23:31, 27 March 2016
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.
TODO Deep difference between slice and array (memory model, etc.)
Declaration
A slice declaration is similar to an array's except the length is not specified. The slice is created with a zero length.
var s []int
Type inferred declaration can be performed using make():
Slice Literals
Slice Operators and Functions
make()
<slice_identifier> := make([]<slice_element_type>, <slice_length>, <capacity>)
s := make([]int, 5) s1 := make([]int, 5, 10)