summaryrefslogtreecommitdiffhomepage
path: root/go.html.markdown
diff options
context:
space:
mode:
authorPhilippe <pvlerick@gmail.com>2014-08-27 17:55:21 +0200
committerPhilippe <pvlerick@gmail.com>2014-08-27 17:55:21 +0200
commitb8ba98c86c78dd4d1a50831c86448ea2c85dcd13 (patch)
treeb418d4c278bf06a1e70251c47f98e924509ef377 /go.html.markdown
parentbfa04112e8e788bea9dc53cdef1659961c7882cb (diff)
parent013112b9b338d5f05d33e6d1d85e39e7f061285d (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'go.html.markdown')
-rw-r--r--go.html.markdown16
1 files changed, 15 insertions, 1 deletions
diff --git a/go.html.markdown b/go.html.markdown
index 656b1051..c85209e0 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -78,7 +78,7 @@ func learnTypes() {
can include line breaks.` // Same string type.
// Non-ASCII literal. Go source is UTF-8.
- g := 'Σ' // rune type, an alias for uint32, holds a unicode code point.
+ g := 'Σ' // rune type, an alias for int32, holds a unicode code point.
f := 3.14195 // float64, an IEEE-754 64-bit floating point number.
c := 3 + 4i // complex128, represented internally with two float64's.
@@ -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.