# 建立與存取陣列

``````package main

import "fmt"

func main() {
var scores [10]int
scores[0] = 90
scores[1] = 89
fmt.Println(scores)      // [90 89 0 0 0 0 0 0 0 0]
fmt.Println(len(scores)) // 10
}
``````

``````package main

import "fmt"

func main() {
arr1 := [3]int{1, 2, 3}
arr2 := [5]int{1, 2, 3}
arr3 := [...]int{1, 2, 3, 4, 5}
fmt.Println(arr1) // [1 2 3]
fmt.Println(arr2) // [1 2 3 0 0]
fmt.Println(arr3) // [1 2 3 4 5]
}
``````

# 陣列指定與比較

``````package main

import "fmt"

func main() {
arr1 := [...]int{1, 2, 3}
arr2 := arr1
fmt.Println(arr1) // [1 2 3]
fmt.Println(arr2) // [1 2 3]
arr1[0] = 10
fmt.Println(arr1) // [10 2 3]
fmt.Println(arr2) // [1 2 3]
}
``````

# 巢狀陣列

Go 的陣列是線性的，如果想模擬多維，可以使用巢狀陣列。例如，建立一個二維陣列：

``````package main

import "fmt"

func main() {
var arr [2][3]int
fmt.Println(arr)   // [[0 0 0] [0 0 0]]
}
``````

``````package main

import "fmt"

func change(arr [3]int) [3]int {
arr[0] = 10
return arr
}

func main() {
arr1 := [2][3]int{[3]int{1, 2, 3}, [3]int{4, 5, 6}}
fmt.Println(arr1) // [[1 2 3] [4 5 6]]

arr2 := [...][3]int{[...]int{1, 2, 3}, [...]int{4, 5, 6}}
fmt.Println(arr2) // [[1 2 3] [4 5 6]]

arr3 := [2][3]int{{1, 2, 3}, {4, 5, 6}}
fmt.Println(arr3) // [[1 2 3] [4 5 6]]

arr4 := [...][3]int{{1, 2, 3}, {4, 5, 6}}
fmt.Println(arr4) // [[1 2 3] [4 5 6]]
}
``````

# 走訪陣列

``````package main

import "fmt"

func main() {
arr := [...]int{1, 2, 3}
for i := 0; i < len(arr); i++ {
fmt.Printf("%d\n", arr[i])
}
}
``````

``````package main

import "fmt"

func main() {
arr := [...]int{1, 2, 3}
for index, element := range arr {
fmt.Printf("%d: %d\n", index, element)
}
}
``````

``````package main

import "fmt"

func main() {
arr := [...]int{1, 2, 3}
for _, element := range arr {
fmt.Printf("%d\n", element)
}
}
``````