x
1
// In Go, an _array_ is a numbered sequence of elements of a
2
// specific length.
3
4
package main
5
6
import "fmt"
7
8
func main() {
9
10
// Here we create an array `a` that will hold exactly
11
// 5 `int`s. The type of elements and length are both
12
// part of the array's type. By default an array is
13
// zero-valued, which for `int`s means `0`s.
14
var a [5]int
15
fmt.Println("emp:", a)
16
17
// We can set a value at an index using the
18
// `array[index] = value` syntax, and get a value with
19
// `array[index]`.
20
a[4] = 100
21
fmt.Println("set:", a)
22
fmt.Println("get:", a[4])
23
24
// The builtin `len` returns the length of an array.
25
fmt.Println("len:", len(a))
26
27
// Use this syntax to declare and initialize an array
28
// in one line.
29
b := [5]int{1, 2, 3, 4, 5}
30
fmt.Println("dcl:", b)
31
32
// Array types are one-dimensional, but you can
33
// compose types to build multi-dimensional data
34
// structures.
35
var twoD [2][3]int
36
for i := 0; i < 2; i++ {
37
for j := 0; j < 3; j++ {
38
twoD[i][j] = i + j
39
}
40
}
41
fmt.Println("2d: ", twoD)
42
}
43