数组跟切片的区别

从3)、4)可见,创建切片跟创建数组唯一的区别在于 Type 前的“ [] ”中是否有数字,为空,则代表切片,否则则代表数组。因为切片是长度可变的。如下是创建切片的示例:

复制代码
func test8() {
    slice1 := make([]int32, 5, 8)
    slice2 := make([]int32, 9)
    slice3 := []int32{}
    slice4 := []int32{1, 2, 3, 4, 5}
    fmt.Println(slice1)
    fmt.Println(slice2)
    fmt.Println(slice3)
    fmt.Println(slice4)
}
复制代码

输出为:

[0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[]
[1 2 3 4 5]

 如上,创造了4个切片,3个空切片,一个有值的切片。

转自:https://www.cnblogs.com/zrtqsk/p/4148495.html

原文地址:https://www.cnblogs.com/wwwsss/p/14911721.html