remove item from array in Go
Code snippet on how to remove item from array in Go
// Create a slice
nums := []int{1,2,3,4,5,6}
// Find the element to be removed
var remove int = 2
// Find the index of the element to be removed
index := 0
for i, v := range nums {
if v == remove {
index = i
break
}
}
// Slice the original slice from 0 to index,
// thus removing the element
nums = append(nums[:index], nums[index+1:]...)
```
This code removes an item from an existing slice in Go. First, it creates a slice of integers (nums) with values 1, 2, 3, 4, 5 and 6. Then, it assigns the value to be removed (2) to a variable called remove. It then uses a loop to search through each item in the nums slice, until it finds the item that matches the value of remove. Once found, it stores the index of the item in the index variable. Finally, it uses the append function to slice the original slice from 0 to the index stored in index, minus the item at the index, thus removing it from the slice.