FAQ
1.
copy(s[i:],s[i+:])
s[len(s)-1]=nil

2.
s = append(s[:i],s[i+1:])

which is better?

--
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

  • Konstantin Khomoutov at Jan 13, 2015 at 2:44 pm

    On Tue, 13 Jan 2015 06:38:14 -0800 (PST) Gao wrote:

    1.
    copy(s[i:],s[i+:])
    s[len(s)-1]=nil

    2.
    s = append(s[:i],s[i+1:])

    which is better?
    You might consider using a linked list [1] or a map instead.

    1. http://golang.org/pkg/container/list/

    --
    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.
  • Gao at Jan 13, 2015 at 3:12 pm
    I think if a func returns `container.List` is less explicit than a func
    retuning `[]ele`.

    On Tuesday, January 13, 2015 at 10:44:39 PM UTC+8, Konstantin Khomoutov
    wrote:
    On Tue, 13 Jan 2015 06:38:14 -0800 (PST)
    Gao <gaop...@gmail.com <javascript:>> wrote:
    1.
    copy(s[i:],s[i+:])
    s[len(s)-1]=nil

    2.
    s = append(s[:i],s[i+1:])

    which is better?
    You might consider using a linked list [1] or a map instead.

    1. http://golang.org/pkg/container/list/
    --
    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.
  • James Bardin at Jan 13, 2015 at 3:15 pm

    On Tuesday, January 13, 2015 at 9:44:39 AM UTC-5, Konstantin Khomoutov wrote:

    On Tue, 13 Jan 2015 06:38:14 -0800 (PST)
    Gao <gaop...@gmail.com <javascript:>> wrote:
    1.
    copy(s[i:],s[i+:])
    s[len(s)-1]=nil

    This isn't valid syntax, and doesn't properly remove an item. You're
    looking for something like

    a = a[:i+copy(a[i:], a[i+1:])]


    2.
    s = append(s[:i],s[i+1:])
    your append needs and ellipsis [...]

    https://github.com/golang/go/wiki/SliceTricks

    which is better?
    They are essentially identical. I prefer the copy, only because I think
    it's more obvious what you're doing.


    You might consider using a linked list [1] or a map instead.
    That's not really helpful, since one often needs to remove an element from
    a slice even when it is the proper data structure.

    --
    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.
  • Gao at Jan 13, 2015 at 3:43 pm

    On Tuesday, January 13, 2015 at 11:15:01 PM UTC+8, James Bardin wrote:


    On Tuesday, January 13, 2015 at 9:44:39 AM UTC-5, Konstantin Khomoutov
    wrote:
    On Tue, 13 Jan 2015 06:38:14 -0800 (PST)
    Gao wrote:
    1.
    copy(s[i:],s[i+:])
    s[len(s)-1]=nil

    This isn't valid syntax, and doesn't properly remove an item. You're
    looking for something like

    a = a[:i+copy(a[i:], a[i+1:])]


    2.
    s = append(s[:i],s[i+1:])
    your append needs and ellipsis [...]

    https://github.com/golang/go/wiki/SliceTricks
    I didn't check the correctness,and thank for theses tricks.
    which is better?
    They are essentially identical. I prefer the copy, only because I think
    it's more obvious what you're doing.
    Why are they identical?
    You might consider using a linked list [1] or a map instead.
    That's not really helpful, since one often needs to remove an element from
    a slice even when it is the proper data structure.
    --
    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.
  • James Bardin at Jan 13, 2015 at 3:54 pm

    On Tue, Jan 13, 2015 at 10:43 AM, Gao wrote:

    They are essentially identical. I prefer the copy, only because I think
    it's more obvious what you're doing.
    Why are they identical?
    If the the slice has enough capacity (which it always will when removing an
    item), append sets the correct length and copies the data; exactly what
    you're doing with the copy+slice operation.

    --
    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 2:38p
activeJan 13, '15 at 3:54p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase