Well, the capacity will be reduced by one. I don't think this makes much
difference.
On Tuesday, September 20, 2016 at 5:44:20 PM UTC+3, Ian Davis wrote:
>
> On Tue, Sep 20, 2016, at 03:23 PM, Gabriel Adumitrachioaiei wrote:
>
> I don't understand something when I want to pop out first element of a
> slice and use it.
> Here is my version:
>
> s := []int{1,2,3}
> first := s[0]
> s = s[1:]
>
>
> Here is a version that I saw in the standard library:
> https://golang.org/src/database/sql/sql.go#L791
>
> first := s[0]
> copy(s, s[1:])
> s = s[:len(s) - 1]
>
>
> I wonder, why do we need to translate the other elements to the left with
> a copy ?
> In the first case, I guess the first element will still be found in the
> underlying array, and in the second case the last element.
> It's not like for avoiding a memory leak, because neither version
> allocates a new underlying array.
>
>
> If you append to the slice in the second version then the new element will
> reuse the space at the end of the backing array. In the first version Go
> may have to allocate to expand the backing array since there is no spare
> capacity.
>
> Ian
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.