From: Robert Griesemer Date: Thu, 19 Nov 2009 03:15:25 +0000 (-0800) Subject: Allow optional second expression in slice expressions. X-Git-Tag: weekly.2009-12-07~202 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1a8ebcc4b8502460d61fafb09d89a8275f98e547;p=gostls13.git Allow optional second expression in slice expressions. Built-in function copy. Addresses issue 203. R=rsc, r, iant https://golang.org/cl/156089 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index b5e20bcaa5..5eaeea04bc 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -2097,7 +2097,7 @@ PrimaryExpr = Selector = "." identifier . Index = "[" Expression "]" . -Slice = "[" Expression ":" Expression "]" . +Slice = "[" Expression ":" [ Expression ] "]" . TypeAssertion = "." "(" Type ")" . Call = "(" [ ExpressionList ] ")" . @@ -2330,28 +2330,39 @@ a regular assignment to an element of the map.

Slices

-Strings, arrays, and slices can be sliced to construct substrings or descriptors -of subarrays. The index expressions in the slice select which elements appear -in the result. The result has indexes starting at 0 and length equal to the -difference in the index values in the slice. After slicing the array a +For a string, array, or slice a, the primary expression

-a := [4]int{1, 2, 3, 4};
-s := a[1:3];
+a[lo : hi]
 

-the slice s has type []int, length 2, capacity 3, and elements +constructs a substring or slice. The index expressions lo and +hi select which elements appear in the result. The result has +indexes starting at 0 and length equal to +hi - lo. +After slicing the array a +

+ +
+a := [5]int{1, 2, 3, 4, 5};
+s := a[1:4];
+
+ +

+the slice s has type []int, length 3, capacity 4, and elements

 s[0] == 2
 s[1] == 3
+s[2] == 4
 

-The slice length must not be negative. +For convenience, the hi expression may be omitted; the notation +a[lo :] is shorthand for a[lo : len(a)]. For arrays or strings, the indexes lo and hi must satisfy 0 <= lo <= hi <= length; @@ -2461,7 +2472,7 @@ assignment of regular parameters.

 func Split(s string, pos int) (string, string) {
-	return s[0:pos], s[pos:len(s)]
+	return s[0:pos], s[pos:]
 }
 
 func Join(s, t string) string {
@@ -4137,7 +4148,7 @@ The memory is initialized as described in the section on initial values
 (§The zero value).
 

-
+
 new(T)
 
@@ -4170,7 +4181,7 @@ The memory is initialized as described in the section on initial values (§The zero value).

-
+
 make(T [, optional list of expressions])
 
@@ -4199,6 +4210,34 @@ m := make(map[string] int, 100); // map with initial space for 100 elements
+

Copying slices

+ +

+The built-in function copy copies array or slice elements from +a source src to a destination dst and returns the +number of elements copied. Source and destination may overlap. +Both arguments must have the same element type T and must be +assignment compatible to a slice +of type []T. The number of arguments copied is the minimum of +len(src) and len(dst). +

+ +
+copy(dst, src []T) int
+
+ +

+Examples: +

+ +
+var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7};
+var s = make([]int, 6);
+n1 := copy(s, &a);     // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
+n2 := copy(s, s[2:]);  // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
+
+ +

Bootstrapping