In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how go language deletes elements in slices". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to delete elements in slices with go language" can help you solve the problem.
Go language removes elements from slices
The Go language does not provide a special syntax or interface for deleting slice elements, so you need to use the characteristics of the slice itself to delete elements. There are three cases according to the location of the elements to be deleted, namely, delete from the beginning position, delete from the middle position and delete from the tail, of which the element at the end of the slice is deleted the fastest.
Remove from the beginning position
Delete the beginning element to move the data pointer directly:
A = [] int {1,2,3} a = a [1:] / delete the first element a = a [N:] / delete the first N elements
The data pointer may not be moved, but the following data can be moved to the beginning and can be done in place with append (the so-called in-place completion is done within the memory range corresponding to the original slice data, which will not cause changes in the memory space structure):
A = [] int {1,2,3} a = append (a [: 0], a [1:]...) / / Delete the first element a = append (a [: 0], a [N:]...) / / delete the first N elements
You can also use the copy () function to delete the first element:
A = [] int {1,2,3} a = a [: copy (a, a [1:])] / / Delete the first element a = a [: copy (a, a [N:])] / / Delete the first N elements
Remove from the middle position
For deleting the middle element, you need to move the remaining elements as a whole, which can also be done in place with append or copy:
A = [] int {1,2,3,...} a = append (a [: I], a [I + 1:]...) / / remove the middle element a = append (a [: I], a [I + N:]...) / / delete the middle N element a = a [: i+copy (a [I:], a [I + 1:]) / / delete the middle element a = a [: i+copy (a [I:]) A [I + N:]] / / Delete the middle N elements
Remove from the tail
A = [] int {1,2,3} a = a [: len (a)-1] / / Delete the tail 1 element a = a [: len (a)-N] / / delete the tail N elements
Deleting elements at the beginning and at the end can be considered a special case of deleting intermediate elements. Let's take a look at an example.
[example] deletes the element at the specified location of the slice.
Package mainimport "fmt" func main () {seq: = [] string {"a", "b", "c", "d", "e"} / / specify the deletion location index: = 2 / / View the element before and after the deletion position fmt.Println (seq [: index], seq [index+1:]) / / join the elements before and after the deletion point seq = append (seq [: index]) Seq [index + 1:]...) Fmt.Println (seq)}
Code output:
[a b] [d e] [a b d e]
The code is as follows:
Line 1, declare an integer slice and save the string from a to e.
Line 4, for the convenience of demonstration and explanation, use the index variable to save the location of the element that needs to be deleted.
In line 7, seq [: index] represents the first half of the deleted element, with a value of [1 2], and seq [index+1:] represents the second half of the deleted element, with a value of [4 5].
In line 10, connect the two slices using the append () function.
Line 12, output the connected new slice, at which point the element with index 2 has been deleted.
The essence of deleting slice elements in Go language is to reconnect the memory of the front and back parts with the deleted elements as the demarcation point.
This is the end of the introduction to "how the go language deletes elements in slices". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.