奥非域

go数组创建切片[:]

在go中,数组和切片略有不同,不能互换使用;但是,您可以使用运算符轻松地从数组中创建切片。

简而言之,该运算符允许您从数组创建切片,可以选择使用开始和结束边界。例如:[:]

a := [3]int{1, 2, 3, 4} // "a" has type [4]int (array of 4 ints)
x := a[:]   // "x" has type []int (slice of ints) and length 4
y := a[:2]  // "y" has type []int, length 2, values {1, 2}
z := a[2:]  // "z" has type []int, length 2, values {3, 4}
m := a[1:3] // "m" has type []int, length 2, values {2, 3}

长度是数组类型的正式部分(例如 是一个由四个整数组成的数组,不同的大小是不同的类型),而切片可以具有任何长度,包括零。