I’m appending the values of a map into an existing slice.
The code’s like (the s
is a slice that already has some elements in it):
for key, value := range m {
s = append(s, value)
}
As far as I know, slices in Go double their sizes when needed. I could let it double the capacity of itself, but it’ll happen several times a loop, which probably is bad for performance.
In this case, I know the exact space needed, that is, len(m)
. How do I “reserve” the space for a slice, like that in C++? I want the reallocation to happen just once.