初始化:数组需要指定大小,不指定也会根据初始化的自动推算出大小,不可改变
数组:
a := [...]int{1,2,3} a := [3]int{1,2,3}
切片:
a:= []int{1,2,3} a := make([]int, 5) a := make([]int, 5, 10)
函数传递:数组需要明确指定大小,切片不需要。数组是值传递,切片是地址传递
numbers2 := [...]int{1, 2, 3, 4, 5, 6} maxIndex2 := len(numbers2) - 1 for i, e := range numbers2 { if i == maxIndex2 { numbers2[0] += e } else { numbers2[i+1] += e } } fmt.Println(numbers2) numbers3 := []int{1, 2, 3, 4, 5, 6} maxIndex3 := len(numbers3) - 1 for i, e := range numbers3 { if i == maxIndex3 { numbers3[0] += e } else { numbers3[i+1] += e } } fmt.Println(numbers3)
输出:
[7 3 5 7 9 11] [22 3 6 10 15 21]
原文地址:https://www.cnblogs.com/TimLiuDream/p/9898186.html
时间: 2024-12-11 09:12:59