remove elements from slice


Jorge Olivero:
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 add function work?

Seems like it's grabbing everything up to the first element (empty array)

Then append everything after the first element (position zero)

...(dots) What does it do?

Dave:

awhere the slice is , iis the index of the element to remove:

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

...is the syntax for variadic arguments in Go.

Basically, when you define a function, it will put all the arguments you pass into a slice of that type. This way, you can pass any number of arguments (for example, you can pass fmt.Printlnany number of arguments).

Now, when a function is called , it does the opposite: it unpacks a slice and passes it as a separate argument to the variadic function....

So what this line does is:

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

Essentially:

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

Now, you might be wondering, why not do

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

Well, the definition of the function appendis

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

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

Related


remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Jorge Olivero: 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 add function work? Seems

remove elements from slice

Sajan Chandran I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Exp

remove elements from slice

Sajan Chandran: I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Ex

remove elements from slice

Jorge Olivero: 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 add function work? Seems

remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Sajan Chandran I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Exp

remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Jorge Olivero: 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 add function work? Seems

remove elements from slice

Sajan Chandran: I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Ex

Best way to remove selected elements from slice

Akhil K Nambiar I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A. What is the cheapest way to remove all elements in A from B? After some googling, the only way I ca

Best way to remove selected elements from slice

Akhil K Nambiar I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A. What is the cheapest way to remove all elements in A from B? After some googling, the only way I ca

Best way to remove selected elements from slice

Akhil K Nambiar I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A. What is the cheapest way to remove all elements in A from B? After some googling, the only way I ca

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

remove slice elements in for

siritinga iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes: f

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

remove slice elements in for

Siritinga: iThe idiomatic way to remove elements from a slice a(preserving order) seems to be: a = append(a[:i], a[i+1:]...) I'm wondering which is the best way to do this in a loop. As far as I understand it is not possible to use it in the following scopes:

MongoDB slice - remove last n elements from nested array

William Suppose I have the following array: { data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0] } If you want to select all but the last three elements, you can use the solution suggested here db.collection.aggregate([

MongoDB slice - remove last n elements from nested array

William Suppose I have the following array: { data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0] } If you want to select all but the last three elements, you can use the solution suggested here db.collection.aggregate([

MongoDB slice - remove last n elements from nested array

William Suppose I have the following array: { data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0] } If you want to select all but the last three elements, you can use the solution suggested here db.collection.aggregate([

MongoDB slice - remove last n elements from nested array

William Suppose I have the following array: { data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0] } If you want to select all but the last three elements, you can use the solution suggested here db.collection.aggregate([

MongoDB slice - remove last n elements from nested array

William Suppose I have the following array: { data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0] } If you want to select all but the last three elements, you can use the solution suggested here db.collection.aggregate([