英文:
What is the point of slice type in Go?
问题
我已经阅读了这个,但对slice
相对于array
的优势还不完全了解。所以我期望在Stack Overflow上有人能够比它更好地解释,而我相信你能够做到
英文:
I have read this but still not fully aware of the advantage of slice
against array
.So I am expecting somebody in SO explain better than it and I am sure you can
答案1
得分: 25
切片在数组上有很多用途,其中一些已经被其他帖子提到了。
- 切片可以像指针一样进行多种操作。
- 多个切片可以“指向”同一个基本数组。
- 切片是按引用传递的,但由于切片本身是一个指针,因此可以更高效地传递“数组”,因为不需要复制整个数组。
- 然而,与指针不同,切片提供了额外的缓冲区安全性。
- 切片的下溢和上溢会触发异常,而不是允许您不安全地访问内存的其他区域。
- 切片允许您限制对数组的特定区域的访问。这在处理子集时非常有用。
- 切片的长度在运行时动态确定,而数组的大小在编译时固定。此外,切片可以在运行时动态调整大小。
英文:
Slices have a lot of uses over arrays, several of which other posters have already mentioned.
- Slices can operate in many ways like pointers.
- Multiple slices can "point" to the same base array
- Slices are passed by reference, but since a slice itself is a pointer it can be used to pass "arrays" around much more efficiently since the entire array doesn't need to be copied.
- However, unlike pointers, slices provide additional buffer safety
- Slice underflows and overflows trigger exceptions, rather than allowing you an unsafe ability to access other areas of memory.
- Slices allow you to limit access to only certain areas of an array. This can be extremely useful in working with subsets.
- The length of a slice is dynamically determined at runtime, unlike arrays which have their sizes fixed at compile time. Also, slices can be dynamically resized at runtime.
答案2
得分: 14
在go
中,数组是按值传递的;所以,要实现“按引用传递”,你需要使用切片。而且这还不是全部!引用Go的教程:
> 数组的大小是其类型的一部分;然而,你可以声明一个切片变量,将其赋值为相同元素类型的任何数组的指针,或者更常见的是形如a[low : high]的切片表达式,表示由low到high-1索引的子数组。切片看起来很像数组,但没有显式的大小([] vs. [10]),它们引用了底层的、通常是匿名的常规数组的一部分。如果多个切片表示同一数组的不同片段,它们可以共享数据;而多个数组永远不会共享数据。
>
> 在Go程序中,切片比常规数组更常见;它们更灵活,具有引用语义,并且效率更高。它们缺少的是对常规数组的存储布局的精确控制;如果你想在结构体中存储一百个元素的数组,你应该使用常规数组。
>
> 当将数组传递给函数时,你几乎总是希望将形式参数声明为切片。在调用函数时,取数组的地址,Go将创建(高效地)一个切片引用并传递它。
英文:
In go
, arrays are passed by value; so, to "pass by reference", you use a slice. And that's not all! Quoting Go's tutorial:
> The size of the array is part of its
> type; however, one can declare a slice
> variable, to which one can assign a
> pointer to any array with the same
> element type or—much more commonly—a
> slice expression of the form a[low :
> high], representing the subarray
> indexed by low through high-1. Slices
> look a lot like arrays but have no
> explicit size ([] vs. [10]) and they
> reference a segment of an underlying,
> often anonymous, regular array.
> Multiple slices can share data if they
> represent pieces of the same array;
> multiple arrays can never share data.
>
> Slices are much more common in Go
> programs than regular arrays; they're
> more flexible, have reference
> semantics, and are efficient. What
> they lack is the precise control of
> storage layout of a regular array; if
> you want to have a hundred elements of
> an array stored within your structure,
> you should use a regular array.
>
> When passing an array to a function,
> you almost always want to declare the
> formal parameter to be a slice. When
> you call the function, take the
> address of the array and Go will
> create (efficiently) a slice reference
> and pass that.
答案3
得分: 2
我认为在Go博客的这篇文章中更详细地描述了切片和数组的用法。
英文:
I think slices and arrays are described much better and in more detail in this post on the Go Blog.
答案4
得分: 1
除了已经给出的答案之外,切片可以动态调整大小,而数组不能。也就是说,你只能使用常量来指定数组的大小,而可以使用变量来指定切片的大小。
英文:
In addition to the answers already given, slices can be dynamically sized while arrays cannot be. That is, you can only use constants to specify the size of an array, while you can use a variable to specify the size of a slice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论