In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use slicing in the GE language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Slicing is a very frequently used aggregation type in the Go language. It represents a variable-length sequence, and the underlying reference is an array object. A slice consists of three parts: pointer, length, and capacity. The pointer points to the memory address of the underlying array element corresponding to the first element of the slice itself.
The type declaration of the slice is as follows:
Type slice struct {array unsafe.Pointer len int cap int}
Data from the underlying array can be shared among multiple slices, and referenced array intervals may overlap. Using this feature of slices, we can invert, filter, and de-duplicate slices in the original memory space, so that there is no need to declare a slice pointing to the new memory to store the results. as a result, the memory space is saved and the consumption of expanding the underlying array is saved, which is very effective when the slice length is large enough.
The following examples are performed on the memory space of the underlying array of slices, and it is important to note that these operations generate new slices on the underlying array while also changing the underlying array.
Delete the element at the specified location
The following function removes the element at index position I from the original slice
Func remove (slice [] int, I int) [] int {copy (slice [I:], slice [I:]) return slice [: len (slice)-1]} func main () {s: = [] int {5,6,7,8,9} fmt.Println (remove (s, 2)) / / "[56 8 9]"}
The built-in copy function makes it easy to copy one slice to another slice of the same type.
Filter element
The following function filters out the slice elements that meet the criteria from the input source slice and returns a new slice consisting of elements that meet the criteria.
Type funcType func (T) bool / / represents the filtering logic function, and can implement func filter (a [] T, f funcType) [] T {b: = a [: 0] for _, x: = range a {if f (x) {b = append (b, x)}} return b} as needed.
Reverse slice
Func reverse (a [] T) [] T {for I: = len (a) / 2-1; I > = 0; iRaq-{opp: = len (a)-1Mui I [I], a [opp] = a [opp], a [I]} return a}
Group slicing
The following function receives a source slice actions of type [] int and returns a nested slice grouped according to the specified length (it is difficult to explain. Students who have used PHP can be understood as the Go version of the array_chunk function. For those who have not used it, see the example below). Suppose the section value is: [] int {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, set the element length batchSize in the packet to 3, and the slice returned after the function call is [[0 12] [3 45] [6 7 8] [9]]
Func chunk (actions [] int, batchSize int) [] int {var batches [] [] int for batchSize < len (actions) {actions, batches = actions [batchSize:], append (batches, actions [0:batchSize:batchSize])} batches = append (batches, actions) return batches} func main () {actions: = [] int {0,1,2,3,4,5,6,7,8,9} batchSize: = 3 chunks = chunk (actions) BatchSize) / / chunks is [[0 12] [3 45] [6 7 8] [9]]}
By the way, the complete slice expression is as follows:
Input [low:high:max]
The purpose of the last max is that the cap (capacity) of the generated slice is max-low.
De-weight in place (for comparable slice types only)
Import "sort" func main () {in: = [] int {3pje 2pje 1pje 4pje 1} / / any item can be sorted sort.Ints (in) j: = 0 for I: = 1; I < len (in); iBo + {if in [j] = = in [I] {continue} jade + in [j] = in [I]} result: = in [: jagger 1] fmt.Println (result) / / [1 2 3 4]} Thank you for your reading! This is the end of the article on "how to use slicing in GE language". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.