创建一个没有使用 make 的 Go 切片。

huangapple go评论88阅读模式
英文:

Creating a Go slice without make

问题

这段代码是在Go语言中创建一个切片(slice),而不是数组。在Go语言中,使用[]int表示切片类型。切片是一个动态大小的序列,可以根据需要进行扩展或缩小。在这个例子中,nums是一个切片,包含了整数2、3和4。

英文:
nums := []int{2, 3, 4}

What does this do in go? Am I creating an array or a slice?

From this: https://gobyexample.com/range, it says slice. But I think it is an array.

答案1

得分: 4

由于您没有指定长度,这是一个切片。

数组类型定义指定了长度和元素类型:参见"Go切片:用法和内部原理"

创建一个没有使用 make 的 Go 切片。

切片字面量的声明方式与数组字面量相同,只是省略了元素计数。

虽然可以使用内置函数make创建切片,但您使用了字面形式来创建切片。

创建的切片与数组的内部结构不同:

make([]byte, 5)

创建一个没有使用 make 的 Go 切片。

英文:

Since you didn't specify the length, it is a slice.

An array type definition specifies a length and an element type: see "Go Slices: usage and internals"

创建一个没有使用 make 的 Go 切片。

> A slice literal is declared just like an array literal, except you leave out the element count.

While a slice can be created with the built-in function called make, you used the literal form to create a slice.

The internal of the created slice differs from an array:

make([]byte, 5)

创建一个没有使用 make 的 Go 切片。

答案2

得分: 4

实际上,通过这样做:

nums := []int{2, 3, 4}

你同时创建了一个数组和一个切片。但由于它是一个切片字面量,结果将是切片类型,所以nums的类型是[]int,你可以用以下代码验证:

fmt.Printf("%T", nums) // 输出:[]int

发生的情况是,一个数组将在后台自动创建/分配,长度为3,并用列出的元素进行初始化,然后创建一个引用该数组的切片,这个切片将是表达式的结果。

引用自Go 语言规范:复合字面量

切片字面量描述了整个底层数组字面量。因此,切片字面量的长度和容量是最大元素索引加一。切片字面量的形式为

[]T{x1, x2,  xn}

它是对数组应用切片操作的简写形式:

tmp := [n]T{x1, x2,  xn}
tmp[0 : n]

数组字面量也包括长度,例如:

arr := [3]int{1, 2, 3}
arr2 := [...]int{1, 2, 3} // 长度将由编译器计算
fmt.Printf("%T", arr)  // 输出:[3]int
fmt.Printf("%T", arr2) // 输出:[3]int
英文:

Actually by doing this:

nums := []int{2, 3, 4}

You are creating both: an array and a slice. But since it is a slice literal, the result will be of slice type, so the type of nums is []int which you can verify with this code:

fmt.Printf("%T", nums) // Output: []int

What happens is that an array will be created/allocated automatically in the background with a length of 3 initialized with the listed elements, and a slice will be created referring to the array, and this slice will be the result of the expression.

Quoting from the Go Language Specification: Composite literals:

> A slice literal describes the entire underlying array literal. Thus, the length and capacity of a slice literal are the maximum element index plus one. A slice literal has the form
>
> []T{x1, x2, … xn}
>
> and is shorthand for a slice operation applied to an array:
>
> tmp := [n]T{x1, x2, … xn}
> tmp[0 : n]

An Array literal also includes the length, for example:

arr := [3]int{1, 2, 3}
arr2 := [...]int{1, 2, 3} // Length will be computed by the compiler
fmt.Printf("%T", arr)  // Output: [3]int
fmt.Printf("%T", arr2) // Output: [3]int

答案3

得分: 3

在Go语言中,数组类型包含它们的长度。由于你省略了长度,所以它是一个切片

array := [3]int{1, 2, 3} // 数组,因为它包含了长度(3)。
slice := array[:] // 切片,因为没有指定长度。

fmt.Printf("%#v - %T\n", slice, slice) // %T 表示“类型”。
fmt.Printf("%#v - %T\n", array, array)
// [3]int{1, 2, 3} - [3]int
// []int{1, 2, 3} - []int

在上面的示例中,我们通过将切片设置为array的完整范围而创建了一个切片,而没有使用"make"函数。如果你编辑了arrayslice中的任何一个,两者都会发生变化,因为"slice"本质上是对存储在"array"中的数据的一种视图。

slice[0] = 456 // 现在 array[0] == 456
array[0] = 789 // 现在 slice[0] == 789
英文:

In go, array types include their length. Since you omitted the length it is a slice:

array := [3]int{1, 2, 3} // Array since it includes length (3).
slice := array[:] // Slice since there is no length specified.

fmt.Printf("%#v - %T\n", slice, slice) // %T means "type".
fmt.Printf("%#v - %T\n", array, array)
// [3]int{1, 2, 3} - [3]int
// []int{1, 2, 3} - []int

In the example above, we made a slice without invoking "make" by setting it to the full range of of array. If you were to edit either array or slice then both will change, since "slice" is essentially a view into the storage that is "array".

slice[0] = 456 // And array[0] == 456
array[0] = 789 // And slice[0] == 789

huangapple
  • 本文由 发表于 2015年3月31日 13:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/29361377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定