From 0c494718afb00c4885638658e07f65ed0bc1c5e3 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 28 Sep 2012 15:55:38 -0700 Subject: [PATCH] go spec: arguments for append may overlap Fixes #4142. R=rsc, r, iant, ken, remyoudompheng CC=golang-dev https://golang.org/cl/6567062 --- doc/go_spec.html | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 2b9f14428b1..de35425b3b1 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -4903,7 +4903,10 @@ m := make(map[string]int, 100) // map with initial space for 100 elements

Appending to and copying slices

-Two built-in functions assist in common slice operations. +The built-in functions append and copy assist in +common slice operations. +For both functions, the result is independent of whether the memory referenced +by the arguments overlaps.

@@ -4934,21 +4937,22 @@ slice may refer to a different underlying array.

 s0 := []int{0, 0}
-s1 := append(s0, 2)        // append a single element     s1 == []int{0, 0, 2}
-s2 := append(s1, 3, 5, 7)  // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
-s3 := append(s2, s0...)    // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
+s1 := append(s0, 2)                // append a single element     s1 == []int{0, 0, 2}
+s2 := append(s1, 3, 5, 7)          // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
+s3 := append(s2, s0...)            // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
+s4 := append(s3[3:6], s3[2:]...)   // append overlapping slice    s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
 
 var t []interface{}
-t = append(t, 42, 3.1415, "foo")                          t == []interface{}{42, 3.1415, "foo"}
+t = append(t, 42, 3.1415, "foo")                                  t == []interface{}{42, 3.1415, "foo"}
 
 var b []byte
-b = append(b, "bar"...)  // append string contents      b == []byte{'b', 'a', 'r' }
+b = append(b, "bar"...)            // append string contents      b == []byte{'b', 'a', 'r' }
 

The function copy copies slice elements from a source src to a destination dst and returns the -number of elements copied. Source and destination may overlap. +number of elements copied. Both arguments must have identical element type T and must be assignable to a slice of type []T. The number of elements copied is the minimum of