To remove an item from an array of type int
in Go, we can make use of slicing. Here's an example:
main.go171 chars7 lines
In the above code, we first create an array arr
containing five integers. We then use the append
function to slice the array and remove the item at index 2 (i.e. the 3rd item). The first parameter to append
is the slice of arr
from the start to the index before the item we want to remove, and the second parameter is the slice from the index after the item we want to remove to the end of the array.
Finally, we print the updated array which now contains [1 2 4 5]
- the array with the third item removed.
gistlibby LogSnag