英文:
Is there any better way to handle slices of variable size?
问题
请看下面的代码:
names := make([]string, 0, 100)
names = append(names, "Jack")
names = append(names, "Jacob")
// 在这里添加很多名字
在这种情况下:我将从其他地方获取这些名字,在那之前我不知道它的大小。所以我需要一个动态数组来存储这些名字。上面的代码是我想出来的方法。我想知道是否有更加优雅的方法来做到这一点。
如果我这样初始化:
names := make([]string, 100, 200)
// 然后在这里使用 append
// 我会得到前100个元素为空,append 从索引101开始。
我觉得这样会浪费内存。我对静态编程语言完全不熟悉,所以如果这篇文章中有任何错误的概念,请指出来。
英文:
please see the code below
names := make([]string, 0, 100)
names = append(names, "Jack")
names = append(names, "Jacob")
// adding many names in here
Given a circumstances like this: I will get these names from somewhere else, before that I didn't know the size of it. So I a need a dynamic array to contains these names. The above code is way that I came up with. I was wonder if there is any more elegant way to do this.
If I initialise like this
names := make([]string, 100, 200)
// then I use append in here
// I would get first 100 elements as empty, the append start at index 101.
I suppose this would be a such waste on memory.
I am totally new to static programming language, so if there is any wrong concept in this post, please point it out.
答案1
得分: 7
只需声明类型,然后将附加的切片分配给它:
package main
import "fmt"
func main() {
var names []string
names = append(names, "foo")
names = append(names, "bar")
fmt.Println(names)
}
输出结果为:
>> [foo bar]
如果你对其机制感兴趣,这里有一篇不错的博文。
英文:
Just only declare the type and then assign the appended slice to it:
package main
import "fmt"
func main() {
var names []string
names = append(names, "foo")
names = append(names, "bar")
fmt.Println(names)
}
Yields:
>> [foo bar]
If you are into the mechanics of it, here is a nice blog post.
答案2
得分: 2
坚持你正在做的事情。100 不会阻止切片拥有超过 100 个元素。
names := make([]string, 0, 100)
names = append(names, "Jack")
names = append(names, "Jacob")
我强烈建议如果你对元素数量有大致的估计,就设置切片的容量,并且始终使用 append 来向切片添加元素。你不必担心超出估计范围,因为 append 会创建新的数组来容纳添加的元素。
names := make([]string)
在上述情况中,你的数组容量为 0,而 append 会导致底层数组一次又一次地被创建。这会影响性能。你应该避免这种情况。如果你担心在内存中占用更多空间,可以考虑创建指向某种类型的指针的切片。
objList := make([]*MyStructType, 0, 100)
英文:
Stick with what you are doing. The 100 DOES NOT prevent the slice from having more the 100 elements
names := make([]string, 0, 100)
names = append(names, "Jack")
names = append(names, "Jacob")
I would strongly suggest to set the capacity of the slice if you have rough estimates of number of elements and ALWAYS use append to add elements to the slice. You don't have to worry about the exceeding your estimate as append WILL create new array to fit the added elements.
names := make([]string)
The above case your array has 0 capacity, and append will cause the underlying array to be created again and again. This will have impact in performance. You should avoid this. If you worry about taking up more space in the memory you might consider creating slice of pointer to a type
objList := make([]*MyStructType, 0, 100)
答案3
得分: 0
你可以考虑在容量(100,表示最多可以添加100个元素)之外设置一个初始长度,而不是'0'。
参考“Arrays, slices (and strings): The mechanics of 'append'”
分配。
我们可以使用新的内置函数来分配一个更大的数组,然后切片结果,但是使用内置的make函数更简单。
它一次性分配一个新的数组并创建一个切片头来描述它。make函数接受三个参数:切片的类型、初始长度和容量,容量是make分配的用于保存切片数据的数组的长度。
这个想法是避免append()
过早地扩展切片,特别是如果你知道你将至少接收到n个元素。
英文:
You can consider setting an initial length, instead of '0', in addition of your capacity (100, meaning at most 100 elements can be added).
See "Arrays, slices (and strings): The mechanics of 'append'"
> allocation.
>
> We could use the new built-in function to allocate a bigger array and then slice the result, but it is simpler to use the make built-in function instead.
It allocates a new array and creates a slice header to describe it, all at once. The make function takes three arguments: the type of the slice, its initial length, and its capacity, which is the length of the array that make allocates to hold the slice data
The idea is to avoid append()
to have to grow the slice too soon, especially if you know you will receive at least n elements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论