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

@ -7,7 +7,7 @@ Hello World
# If/Else
# Switch
Arrays
# Slices
Slices
Maps
# Range
# Functions

View File

@ -1,4 +1,4 @@
// In Go, an array is a numbered sequence of elements of a
// In Go, an _array_ is a numbered sequence of elements of a
// specific length.
package main
@ -10,26 +10,26 @@ func main() {
// The type of elements and length are both part of
// the array's type. Here we create an array `x` that
// will hold exactly 5 ints.
var x [5]int
var a [5]int
// By default an array is zero-valued, which for ints
// means an array of `0`s.
fmt.Println("emp:", x)
fmt.Println("emp:", a)
// We can set a value at a given index using the
// `array[index] = value` syntax, and get a value
// with `array[index]`.
x[4] = 100
fmt.Println("set:", x)
fmt.Println("get:", x[4])
a[4] = 100
fmt.Println("set:", a)
fmt.Println("get:", a[4])
// The builtin `len` returns the length of an array.
fmt.Println("len:", len(a))
// Use this syntax to decalare and initalize an array
// in one line.
y := [5]int{1, 2, 3, 4, 4}
fmt.Println("dcl:", y)
// The builtin `len` returns the length of an array.
fmt.Println("len:", len(y))
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)
// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data

View File

@ -4,8 +4,8 @@ $ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
dcl: [1 2 3 4 4]
len: 5
dcl: [1 2 3 4 5]
2d: [[0 1 2] [1 2 3]]
# You'll see _slices_ much more often than arrays in

View File

@ -1,4 +1,4 @@
// [SHA1 hashes](http://en.wikipedia.org/wiki/SHA-1) are
// [_SHA1 hashes_](http://en.wikipedia.org/wiki/SHA-1) are
// frequently used to compute short identities for binary
// or text blobs. For example, the [git revision control
// system](http://git-scm.com/) uses SHA1s extensively to

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.

View File

@ -1,8 +1,8 @@
// [Timers](timers) are for when you want to do
// something once in the future - tickers are for when you
// want to do something repeatedly at regular intervals.
// Here's an example of a ticker that ticks periodically
// until we stop it.
// something once in the future - _tickers_ are for when
// you want to do something repeatedly at regular
// intervals. Here's an example of a ticker that ticks
// periodically until we stop it.
package main

View File

@ -1,6 +1,6 @@
// We often want to execute Go code at some point in the
// future, or repeatedly at some interval. Go's built-in
// timer and ticker features make both of these tasks
// _timer_ and _ticker_ features make both of these tasks
// easy. We'll look first at timers and then
// at [tickers](tickers).