From 0f3e9003a526e96cd1f04f340254088271deed0c Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 25 Oct 2010 16:50:31 -0700 Subject: go spec: append built-in R=iant, ken2, r, rsc CC=golang-dev http://codereview.appspot.com/2627043 --- doc/go_spec.html | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index fc47ae825..3aaa2563f 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1492,8 +1492,8 @@ Zero value: nil Functions: - cap close closed cmplx copy imag len make - new panic print println real recover + append cap close closed cmplx copy imag len + make new panic print println real recover @@ -4527,10 +4527,38 @@ m := make(map[string] int, 100) // map with initial space for 100 elements -

Copying slices

+

Appending to and copying slices

-The built-in function copy copies slice elements from +Two built-in functions assist in common slice operations. +

+ +

+The function append appends zero or more values x +to a slice s and returns the resulting slice. Each value must be +assignable to the slice's element type. +

+ +
+append(s []T, x ...T) []T
+
+ +

+If the capacity of s is not large enough to fit the additional +values, append allocates a new, sufficiently large slice that fits +both the existing slice elements and the additional values. Thus, the returned +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}
+
+ +

+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. Both arguments must have identical element type T and must be -- cgit v1.2.1