1、數組的初始化
示例:
package main //必須有個main包
import "fmt"
func main() {
//聲明定義同時賦值,叫初始化
//1、全部初始化
var a [5]int = [5]int{1, 2, 3, 4, 5}
fmt.Println("a = ", a)
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("b = ", b)
//部分初始化,沒有初始化的元素,自動賦值為0
c := [5]int{1, 2, 3}
fmt.Println("c = ", c)
//指定某個元素初始化
d := [5]int{2: 10, 4: 20} //下標為2的賦值為10,下標為4的賦值為20
fmt.Println("d = ", d)
}
執行結果:
a = [1 2 3 4 5] b = [1 2 3 4 5] c = [1 2 3 0 0] d = [0 0 10 0 20]
