clarify slices re #20. use string values to avoid overloading, specify exaclty what we mean w.r.t. indicies
This commit is contained in:
parent
87fa837f18
commit
c151aa3601
@ -10,15 +10,17 @@ func main() {
|
|||||||
// Unlike arrays, slices are typed only by the
|
// Unlike arrays, slices are typed only by the
|
||||||
// elements they contain (not the number of elements).
|
// elements they contain (not the number of elements).
|
||||||
// To create an empty slice with non-zero length, use
|
// To create an empty slice with non-zero length, use
|
||||||
// the builtin `make`. Here we make a slice of `int`s
|
// the builtin `make`. Here we make a slice of
|
||||||
// of length `5` (initially zero-valued).
|
// `string`s of length `3` (initially zero-valued).
|
||||||
s := make([]int, 5)
|
s := make([]string, 3)
|
||||||
fmt.Println("emp:", s)
|
fmt.Println("emp:", s)
|
||||||
|
|
||||||
// We can set and get just like with arrays.
|
// We can set and get just like with arrays.
|
||||||
s[4] = 100
|
s[0] = "a"
|
||||||
|
s[1] = "b"
|
||||||
|
s[2] = "c"
|
||||||
fmt.Println("set:", s)
|
fmt.Println("set:", s)
|
||||||
fmt.Println("get:", s[4])
|
fmt.Println("get:", s[2])
|
||||||
|
|
||||||
// `len` returns the length of the slice as expected.
|
// `len` returns the length of the slice as expected.
|
||||||
fmt.Println("len:", len(s))
|
fmt.Println("len:", len(s))
|
||||||
@ -29,34 +31,34 @@ func main() {
|
|||||||
// returns a slice containing one or more new values.
|
// returns a slice containing one or more new values.
|
||||||
// Note that we need to accept a return value from
|
// Note that we need to accept a return value from
|
||||||
// append as we may get a new slice value.
|
// append as we may get a new slice value.
|
||||||
s = append(s, 6)
|
s = append(s, "d")
|
||||||
s = append(s, 7, 8)
|
s = append(s, "e", "f")
|
||||||
fmt.Println("apd:", s)
|
fmt.Println("apd:", s)
|
||||||
|
|
||||||
// Slices can also be `copy`'d. Here we create an
|
// Slices can also be `copy`'d. Here we create an
|
||||||
// empty slice `c` of the same length as `s` and copy
|
// empty slice `c` of the same length as `s` and copy
|
||||||
// into `c` from `s`.
|
// into `c` from `s`.
|
||||||
c := make([]int, len(s))
|
c := make([]string, len(s))
|
||||||
copy(c, s)
|
copy(c, s)
|
||||||
fmt.Println("cpy:", c)
|
fmt.Println("cpy:", c)
|
||||||
|
|
||||||
// Slices support a "slice" operator with the syntax
|
// Slices support a "slice" operator with the syntax
|
||||||
// `slice[low:high]`. For example, this gets a slice
|
// `slice[low:high]`. For example, this gets a slice
|
||||||
// of the elements 4, 5, and 6.
|
// of the elements `s[2]`, `s[3]`, and `s[4]`.
|
||||||
l := s[4:7]
|
l := s[2:5]
|
||||||
fmt.Println("sl1:", l)
|
fmt.Println("sl1:", l)
|
||||||
|
|
||||||
// This slices up to the 7th index.
|
// This slices up to (but excluding) `s[5]`.
|
||||||
l = s[:7]
|
l = s[:5]
|
||||||
fmt.Println("sl2:", l)
|
fmt.Println("sl2:", l)
|
||||||
|
|
||||||
// And this slices from the 4th index upwards.
|
// And this slices up from (and including) `s[2]`.
|
||||||
l = s[4:]
|
l = s[2:]
|
||||||
fmt.Println("sl3:", l)
|
fmt.Println("sl3:", l)
|
||||||
|
|
||||||
// We can declare and initialize a variable for slice
|
// We can declare and initialize a variable for slice
|
||||||
// in a single line as well.
|
// in a single line as well.
|
||||||
t := []int{1, 2, 3, 4, 5}
|
t := []string{"g", "h", "i"}
|
||||||
fmt.Println("dcl:", t)
|
fmt.Println("dcl:", t)
|
||||||
|
|
||||||
// Slices can be composed into multi-dimensional data
|
// Slices can be composed into multi-dimensional data
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
# Note that while slices are different types than arrays,
|
# Note that while slices are different types than arrays,
|
||||||
# they are rendered similarly by `fmt.Println`.
|
# they are rendered similarly by `fmt.Println`.
|
||||||
$ go run slices.go
|
$ go run slices.go
|
||||||
emp: [0 0 0 0 0]
|
emp: [ ]
|
||||||
set: [0 0 0 0 100]
|
set: [a b c]
|
||||||
get: 100
|
get: c
|
||||||
len: 5
|
len: 3
|
||||||
apd: [0 0 0 0 100 6 7 8]
|
apd: [a b c d e f]
|
||||||
cpy: [0 0 0 0 100 6 7 8]
|
cpy: [a b c d e f]
|
||||||
sl1: [100 6 7]
|
sl1: [c d e]
|
||||||
sl2: [0 0 0 0 100 6 7]
|
sl2: [a b c d e]
|
||||||
sl3: [100 6 7 8]
|
sl3: [c d e f]
|
||||||
dcl: [1 2 3 4 5]
|
dcl: [g h i]
|
||||||
2d: [[0] [1 2] [2 3 4]]
|
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)
|
# Check out this [great blog post](http://blog.golang.org/2011/01/go-slices-usage-and-internals.html)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user