34 lines
658 B
Go
34 lines
658 B
Go
// array literals are their own types
|
|
// [3]int is a different type to [4]int
|
|
|
|
// dont use arrays unless you know the exact length ahead of time
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
var x [3]int
|
|
|
|
x = [3]int{1, 2, 3}
|
|
fmt.Println(x)
|
|
|
|
// index 0=1, index 5=4, index 6=6, index 10=100, index 11=15, the rest 0
|
|
var y = [12]int{1, 5: 4, 6, 10: 100, 15}
|
|
fmt.Println(y)
|
|
|
|
// can leave the size off if initialising an array
|
|
var z = [...]int{1, 2, 3}
|
|
|
|
// comparison
|
|
fmt.Println(z == x)
|
|
|
|
// an array of length 2, of type array length 3 (2x3 matrix)
|
|
var m [2][3]int
|
|
fmt.Println(m)
|
|
|
|
// accessing elements
|
|
fmt.Println(x[0])
|
|
fmt.Println(x[len(x)-1])
|
|
}
|