csdnceshi77
狐狸.fox
2014-07-29 21:36
采纳率: 0%
浏览 128

切片中的删除元素

func main() {
    a := []string{"Hello1", "Hello2", "Hello3"}
    fmt.Println(a)
    // [Hello1 Hello2 Hello3]
    a = append(a[:0], a[1:]...)
    fmt.Println(a)
    // [Hello2 Hello3]
}

How does this delete trick with the append function work?

It would seem that it's grabbing everything before the first element (empty array)

Then appending everything after the first element (position zero)

What does the ... (dot dot dot) do?

转载于:https://stackoverflow.com/questions/25025409/delete-element-in-a-slice

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • csdnceshi71
    Memor.の 2014-07-29 21:47
    已采纳

    Where a is the slice, and i is the index of the element you want to delete:

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

    ... is syntax for variadic arguments in Go.

    Basically, when defining a function it puts all the arguments that you pass into one slice of that type. By doing that, you can pass as many arguments as you want (for example, fmt.Println can take as many arguments as you want).

    Now, when calling a function, ... does the opposite: it unpacks a slice and passes them as separate arguments to a variadic function.

    So what this line does:

    a = append(a[:0], a[1:]...)
    

    is essentially:

    a = append(a[:0], a[1], a[2])
    

    Now, you may be wondering, why not just do

    a = append(a[1:]...)
    

    Well, the function definition of append is

    func append(slice []Type, elems ...Type) []Type
    

    So the first argument has to be a slice of the correct type, the second argument is the variadic, so we pass in an empty slice, and then unpack the rest of the slice to fill in the arguments.

    点赞 评论
  • csdnceshi80
    胖鸭 2014-07-30 02:52

    ... is syntax for variadic arguments.

    I think it is implemented by the complier using slice ([]Type), just like the function append :

    func append(slice []Type, elems ...Type) []Type
    

    when you use "elems" in "append", actually it is a slice([]type). So "a = append(a[:0], a[1:]...)" means "a = append(a[0:0], a[1:])"

    a[0:0] is a slice which has nothing

    a[1:] is "Hello2 Hello3"

    This is how it works

    点赞 评论
  • csdnceshi69
    YaoRaoLov 2016-05-21 06:30

    There are two options:

    A: You care about retaining array order:

    a = append(a[:i], a[i+1:]...)
    // or
    a = a[:i+copy(a[i:], a[i+1:])]
    

    B: You don't care about retaining order (this is probably faster):

    a[i] = a[len(a)-1] // Replace it with the last one.
    a = a[:len(a)-1]   // Chop off the last one.
    

    See the link to see implications re memory leaks if your array is of pointers.

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

    点赞 评论
  • csdnceshi70
    笑故挽风 2017-05-01 22:44

    Or since you're trying to find the index of the element that's to be deleted anyway,

    // na = new a, da = a that's to be deleted
    var na []string
    for _, v := range a {
        if v == da {
            continue
        } else {
            na = append(na, v)
        }
    }
    a = na
    

    Ok never mind. Right answer for the topic, but wrong answer for the question body.

    点赞 评论
  • csdnceshi56
    lrony* 2018-02-25 12:30

    In golang's wiki it show some tricks for slice, including delete an element from slice.

    Link: enter link description here

    For example a is the slice which you want to delete the number i element.

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

    OR

    a = a[:i+copy(a[i:], a[i+1:])]
    
    点赞 评论

相关推荐