publish slices, tweak surrounding

This commit is contained in:
Mark McGranaghan
2012-10-10 21:53:00 -07:00
parent 4549dbcaca
commit c489746ec7
8 changed files with 111 additions and 27 deletions

View File

@@ -1,14 +1,77 @@
// _Slices_ are a key data type in Go, giving a more
// powerful interface to sequences that arrays.
package main
import "fmt"
func main() {
slice1 := []int{1, 2, 3}
slice2 := append(slice1, 4, 5)
fmt.Println(slice1)
fmt.Println(slice2)
slice3 := make([]int, 2)
copy(slice3, slice1)
fmt.Println(slice1)
fmt.Println(slice3)
// In contrast to arrays, slices are typed only
// by the elements it contains (not the number of
// elements). To create an empty slice with non-zero
// length, use the builtin `make`. Here we make a
// slice of `int`s of length 5.
s := make([]int, 5)
// New slices are initially empty-valued.
fmt.Println("emp:", s)
// We can set and get just like with arrays.
s[4] = 100
fmt.Println("set:", s)
fmt.Println("get:", s[4])
// `len` returns the length of the slice as expected.
fmt.Println("len:", len(s))
// In addition to these basic operations, slices
// support several more that make them richer than
// arrays. One is the builtin `append`, which
// returns a slice containing one or more new values.
// Note that we need to accapt a return value from
// append as we may get a new slice reference.
s = append(s, 6)
s = append(s, 7, 8)
fmt.Println("apd:", s)
// Slices can also be `copy`'d. Here we create an
// empty slice `c` of the same length as `s` and copy
// into `c` from `s`.
c := make([]int, len(s))
copy(c, s)
fmt.Println("cpy:", c)
// Slices support a "slice" operator, which is denoted
// with brackets containing a `:`. For example, this
// get a slice of the elements 4, 5, and 6.
l := s[4:7]
fmt.Println("sl1:", l)
// To slice up the 7th index.
l = s[:7]
fmt.Println("sl2:", l)
// Or from the 4th index upwards.
l = s[4:]
fmt.Println("sl3:", l)
// We can declare and initalize a slice in a single
// line as well.
t := []int{1, 2, 3, 4, 5}
fmt.Println("dcl:", t)
// Slices can be composed into multi-dimensional data
// structures. The length of the inner slices can
// vary, unlike in the case of multi-dimensional
// arrays.
twoD := make([][]int, 3)
for i := 0; i < 3; i++ {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := 0; j < innerLen; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}

21
examples/slices/slices.sh Normal file
View File

@@ -0,0 +1,21 @@
# Note that while slices are different types than arrays,
# they are rendered similarly by `fmt.Println`.
$ go run slices.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
apd: [0 0 0 0 100 6 7 8]
cpy: [0 0 0 0 100 6 7 8]
sl1: [100 6 7]
sl2: [0 0 0 0 100 6 7]
sl3: [100 6 7 8]
dcl: [1 2 3 4 5]
2d: [[0] [1 2] [2 3 4]]
# Check out this [great blog post](http://blog.golang.org/2011/01/go-slices-usage-and-internals.html)
# by the Go team for more details on the design and
# implementation of slices in Go.
# Now that we've seen arrays and slices we'll look at
# Go's other key builtin data structure: maps.