You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays.
在Go中一个数组的长度是固定的,但是切片的长度是可以动态扩充的,所以在实际应用中切片比数组更常用。
声明切片
切片在未初始化前默认的zero value是nil, 长度是0.
初始化切片
从已知数组初始化切片
var slice1 []type = array1[start:end]
表示 slice1 是由数组 arr1 从 start 索引到 end-1 索引之间的元素构成的子集(切分数组,start:end 被称为 slice 表达式)
初始化相关数组和切片
var x = []int{2, 3, 5, 7, 11}
这样就创建了一个长度为 5 的数组并从这个相关数组创建了一个切片。用make初始化
当相关数组还没有定义时,用 make() 函数来创建一个切片 同时创建好相关数组
make([]type)
make([]type, len)
make([]type, len, cap)
引用类型
slice
是引用类型,它的零值是nil, 长度是0.The text was updated successfully, but these errors were encountered: