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 Area struct {
    Cells [2][]uint8
}
func main() {
    var area1 Area
    area1.Cells[1] = []uint8 {5, 6, 7}

    area2 := area1

    area1.Cells[1] = removeValueFromCell(area1.Cells[1], 6)

    fmt.Println(area1.Cells[1])
    fmt.Println(area2.Cells[1])
}


func removeValueFromCell(cell []uint8, value uint8) []uint8{
    var res = cell
    for i := 0; i < len(cell); i++ {
        if cell[i] == value {
            res = append(cell[:i], cell[i+1:]...)
        }
    }
    return res
}

The program outputs:

[5 7] <- as expected

[5 7 7] <- why not [5 6 7] or [5 7] ?
icza :

The slice value is just the header, pointing to the backing array. Slice headers only contain pointers. So when you copy the slice value, the copy will also point to the same backing array. So if you change the backing array via the original slice header, the copy will also observe the change.

This is your case. you assign area1to area2. A cell is an array of slices. So the array containing the slice headers will be copied, so the slice headers will be copied. The slice header contains a pointer to the backing array, which will not be repeated.

Therefore, there is only one backing array that supports [5, 6, 7]elements . Then call removeValueFromCell(), it will modify this backing array:

Before:
[5, 6, 7]
After:
[5, 7, 7]

Because the element 6has been removed, the rest of the slice (the elements ) will be [7]copied in place of the removed element.

Then assign this new slice header (which correctly contains only 2 elements) to area1.Cells[1].

But the shard value area2.Cells[1]points to the same backing array, and since you didn't touch this shard value, it still has length 3, so it will see all backing arrays changed elements: [5, 7, 7].

Also note that your implementation removeValueFromCell()is wrong because the movable element will behave incorrectly if it will be listed multiple times in the slice. The reason for this is because when you remove an element, the indices of subsequent elements are shifted (decremented by 1), but loop variables don't fix that. The easiest fix is ​​to use a downward loop. See how to remove elements of an array of structs in a loop in golang for details .

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

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

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