Home >Backend Development >Golang >How to delete slice elements in go language
How to delete slice elements in Go language: 1. Specify the deletion position, code such as "index := 2"; 2. View the elements before and after the deletion position; 3. Delete the elements before and after the point Just connect them.
The operating environment of this article: windows10 system, Go 1.11.2, thinkpad t480 computer.
How to delete slice elements in Go language?
Go language does not provide special syntax or interface for deleting slice elements. You need to use the characteristics of the slice itself to delete elements. The sample code is as follows:
Implementation code:
Code output result:
[a b] [d e] [a b d e]
Line 1, declare an integer Slice, holds a string containing from a to e.
Line 4, for the convenience of demonstration and explanation, use the index variable to save the position of the element that needs to be deleted.
In line 7: seq[:index] represents the first half of the deleted element, the value is: [1 2]
seq[index 1:] represents the deleted element The second half of the element, the value is: [4 5]
Line 10 uses the append() function to connect the two slices. Line 12, outputs the new connected slices. At this point, the element with index 2 has been deleted.
Related recommendations: golang tutorial
The above is the detailed content of How to delete slice elements in go language. For more information, please follow other related articles on the PHP Chinese website!