aboutsummaryrefslogtreecommitdiff
path: root/content/slices/prog150.go
diff options
context:
space:
mode:
Diffstat (limited to 'content/slices/prog150.go')
-rw-r--r--content/slices/prog150.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/content/slices/prog150.go b/content/slices/prog150.go
new file mode 100644
index 0000000..986b1ce
--- /dev/null
+++ b/content/slices/prog150.go
@@ -0,0 +1,36 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START OMIT
+ // Create a couple of starter slices.
+ slice := []int{1, 2, 3}
+ slice2 := []int{55, 66, 77}
+ fmt.Println("Start slice: ", slice)
+ fmt.Println("Start slice2:", slice2)
+
+ // Add an item to a slice.
+ slice = append(slice, 4)
+ fmt.Println("Add one item:", slice)
+
+ // Add one slice to another.
+ slice = append(slice, slice2...)
+ fmt.Println("Add one slice:", slice)
+
+ // Make a copy of a slice (of int).
+ slice3 := append([]int(nil), slice...)
+ fmt.Println("Copy a slice:", slice3)
+
+ // Copy a slice to the end of itself.
+ fmt.Println("Before append to self:", slice)
+ slice = append(slice, slice...)
+ fmt.Println("After append to self:", slice)
+ // END OMIT
+}