Go does not provide any high level functions to remove elements from a slice. I wrote a function that removes given value from a slice in a way that typically suggested here, but it produced quite unexpected result.
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
}
This program outputs:
[5 7] <- as expected
[5 7 7] <- why not [5 6 7] or [5 7] ?