From: Robert Griesemer
-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
-the slice
+the slice
-The slice length must not be negative.
+For convenience, the Slices
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]
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];
+
+
+s
has type []int
, length 3, capacity 4, and elements
s[0] == 2
s[1] == 3
+s[2] == 4
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 elementsCopying slices
+ ++The built-in function
+ +copy
copies array or slice elements from +a sourcesrc
to a destinationdst
and returns the +number of elements copied. Source and destination may overlap. +Both arguments must have the same element typeT
and must be +assignment compatible to a slice +of type[]T
. The number of arguments copied is the minimum of +len(src)
andlen(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