summaryrefslogtreecommitdiffhomepage
path: root/go.html.markdown
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2014-08-21 17:12:18 -0500
committerLevi Bostian <levi.bostian@gmail.com>2014-08-21 17:12:18 -0500
commit1bb6478d0829b6925855e05d379a1c344ff0eb44 (patch)
tree16e6bfedb2b5eb3029084836d91d4c137d7d6de0 /go.html.markdown
parent4c4e52356c11168b30e0bc9938f2ec1c62252485 (diff)
parentd951a3bba90bd4b4a7deb8d8a3f56d42fe43384a (diff)
Merge pull request #720 from szaydel/master
append() function is not mentioned in Slices section
Diffstat (limited to 'go.html.markdown')
-rw-r--r--go.html.markdown14
1 files changed, 14 insertions, 0 deletions
diff --git a/go.html.markdown b/go.html.markdown
index f7bd8ee3..c85209e0 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -101,6 +101,20 @@ can include line breaks.` // Same string type.
var d2 [][]float64 // Declaration only, nothing allocated here.
bs := []byte("a slice") // Type conversion syntax.
+ // Because they are dynamic, slices can be appended to on-demand.
+ // To append elements to a slice, built-in append() function is used.
+ // First argument is a slice to which we are appending. Commonly,
+ // the array variable is updated in place, as in example below.
+ s := []int{1, 2, 3} // Result is a slice of length 3.
+ s = append(s, 4, 5, 6) // Added 3 elements. Slice now has length of 6.
+ fmt.Println(s) // Updated slice is now [1 2 3 4 5 6]
+ // To append another slice, instead of list of atomic elements we can
+ // pass a reference to a slice or a slice literal like this, with a
+ // trailing elipsis, meaning take a slice and unpack its elements,
+ // appending them to slice s.
+ s = append(s, []int{7, 8, 9}...) // Second argument is a slice literal.
+ fmt.Println(s) // Updated slice is now [1 2 3 4 5 6 7 8 9]
+
p, q := learnMemory() // Declares p, q to be type pointer to int.
fmt.Println(*p, *q) // * follows a pointer. This prints two ints.