FAQ
I know it is easy to remove an element in slice using:
   x = append(x[:n], x[n+1:]...)

But when slices contain pointers, I often forget to set the last element to
nil, which causes it not to be garbage collected.
So I must add this line to clear the last element:
   x[:n+1][n] = nil

       https://play.golang.org/p/m1Dc2YZDpZ

But it is too easy to forget it.
Would a new "remove" builtin useful for this operation ?
Thus, the decision about setting the last element to nil would be more
explicit.

Any opinion ?

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Jan 13, 2015 at 8:34 pm
    There are a number of problems with this

    1. not all slice elements are pointer types
    2. delete can be implemented more efficiently, without copying, by
    overwriting the deleted element with the final from the slice, then
    reducing the length of the slice. This does affect the ordering of course,
    but is more efficient for large slices. remove() could only do one of
    these, which one to choose ?

    Instead, what about this form to ensure a new slice is created ?

    http://play.golang.org/p/OCpSzMZjnA
    On Wednesday, 14 January 2015 06:05:34 UTC+11, nicolas riesch wrote:

    I know it is easy to remove an element in slice using:
    x = append(x[:n], x[n+1:]...)

    But when slices contain pointers, I often forget to set the last element
    to nil, which causes it not to be garbage collected.
    So I must add this line to clear the last element:
    x[:n+1][n] = nil

    https://play.golang.org/p/m1Dc2YZDpZ

    But it is too easy to forget it.
    Would a new "remove" builtin useful for this operation ?
    Thus, the decision about setting the last element to nil would be more
    explicit.

    Any opinion ?
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 13, '15 at 7:05p
activeJan 13, '15 at 8:34p
posts2
users2
websitegolang.org

2 users in discussion

Dave Cheney: 1 post Nicolas riesch: 1 post

People

Translate

site design / logo © 2021 Grokbase