-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrays.go
63 lines (49 loc) · 1.78 KB
/
arrays.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
)
func main() {
// creates an array of five ints.
// specified length must be a compile-time constant expression.
// this allows compiler to do efficient bounds checking.
var a [5]int
// since length is compile-time constant, len() is a compile time constant
// and does not have the overhead of a function call.
fmt.Println("len(a) =", len(a))
// elements are always initialized to 0
fmt.Println("a =", a)
// assign a value to an element. indexing is 0 based.
a[0] = 3
fmt.Println("a =", a)
// retrieve element value with same syntax
fmt.Println("a[0] =", a[0])
// a slice references an underlying array
s := a[:4] // this does not allocate new array space.
fmt.Println("s =", s)
// slices have runtime established length and capacity, but len() and
// cap() are built in to the compiler and have overhead more like
// variable access than function call.
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// slices can be resliced, as long as there is space
// in the underlying array.
s = s[:5]
fmt.Println("s =", s)
// s still based on a
a[0] = 22
fmt.Println("a =", a)
fmt.Println("s =", s)
// append will automatically allocate a larger underlying array as needed.
s = append(s, 4, 5, 6)
fmt.Println("s =", s)
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// s no longer based on a
a[4] = -1
fmt.Println("a =", a)
fmt.Println("s =", s)
// make creates a slice and allocates a new underlying array
s = make([]int, 8)
fmt.Println("s =", s)
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// the cap()=10 array is no longer referenced
// and would be garbage collected eventually.
}