From e333b96529da99e40fe920ecf084dea4e18309ef Mon Sep 17 00:00:00 2001
From: Robert Griesemer a
can be discovered using the
built-in function cap(a)
.
nil
map causes a
-+Slice expressions construct a substring or slice from a string, array, pointer +to array, or slice. There are two variants: a simple form that specifies a low +and high bound, and a full form that also specifies a bound on the capacity. +
+ +
For a string, array, pointer to array, or slice a
, the primary expression
@@ -2695,6 +2705,53 @@ If the sliced operand of a valid slice expression is a nil
slice, t
is a nil
slice.
+For an array, pointer to array, or slice a
(but not a string), the primary expression
+
+a[low : high : max] ++ +
+constructs a slice of the same type, and with the same length and elements as the simple slice
+expression a[low : high]
. Additionally, it controls the resulting slice's capacity
+by setting it to max - low
. Only the first index may be omitted; it defaults to 0.
+After slicing the array a
+
+a := [5]int{1, 2, 3, 4, 5} +t := a[1:3:5] ++ +
+the slice t
has type []int
, length 2, capacity 4, and elements
+
+t[0] == 2 +t[1] == 3 ++ +
+As for simple slice expressions, if a
is a pointer to an array,
+a[low : high : max]
is shorthand for (*a)[low : high : max]
.
+If the sliced operand is an array, it must be addressable.
+
+The indices are in range if 0 <= low <= high <= max <= cap(a)
,
+otherwise they are out of range.
+A constant index must be non-negative and representable by a value of type
+int
.
+If multiple indices are constant, the constants that are present must be in range relative to each
+other.
+If the indices are out of range at run time, a run-time panic occurs.
+