Golang unset or delete element item in array

0
343
views

There is no function to unset the element in array, but we can use the slice.

In example

We want to remove the 3rd index, become this.

So define indexRemove first.

Then we slice from index 0, and get until index 2. Example.

Then slice from index 4 to last index 9. Example.

Then combine or append into one array. Example.

But it show some error because 2nd parameter in method append() want to …Type. So we change the code. Reference.

The the logic is append first slice and last slice. And we can simplify like this.

 

 

LEAVE A REPLY

Please enter your name here
Please enter your comment!