From e333b96529da99e40fe920ecf084dea4e18309ef Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 11 Sep 2013 17:18:52 -0700 Subject: [PATCH] spec: define s[i:j:k] R=rsc, r, iant, ken CC=golang-dev https://golang.org/cl/10243046 --- doc/go_spec.html | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 12f43ef2fb7..9c2923462b4 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -869,7 +869,7 @@ The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by -slicing a new one from the original slice. +slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a).

@@ -2359,7 +2359,9 @@ PrimaryExpr = Selector = "." identifier . Index = "[" Expression "]" . -Slice = "[" [ Expression ] ":" [ Expression ] "]" . +Slice = "[" ( [ Expression ] ":" [ Expression ] ) | + ( [ Expression ] ":" Expression ":" Expression ) + "]" . TypeAssertion = "." "(" Type ")" . Call = "(" [ ArgumentList [ "," ] ] ")" . ArgumentList = ExpressionList [ "..." ] . @@ -2621,7 +2623,15 @@ Assigning to an element of a nil map causes a

-

Slices

+

Slice expressions

+ +

+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. +

+ +

Simple slice expressions

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.

+

Full slice expressions

+ +

+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. +

+

Type assertions