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 > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to define and initialize Go language slices". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Go language slicing is an abstraction of arrays.
Go array length can not be changed, in a specific scene such a collection is not suitable, Go provides a flexible, powerful built-in type slice ("dynamic array"), the length of the slice is not fixed compared with the array, you can add elements, when appended may increase the capacity of the slice.
Define slicing
You can declare an array of unspecified sizes to define slices:
Var identifier [] type
The length of the slice does not need to be specified.
Or use the make () function to create a slice:
Var slice1 [] type = make ([] type, len) can also be abbreviated as slice1: = make ([] type, len)
You can also specify the capacity, where capacity is an optional parameter.
Make ([] T, length, capacity)
Where len is the length of the array and also the initial length of the slice.
Slice initialization s: = [] int {1pm 2pm 3}
Initialize the slice directly, and [] indicates that it is the slice type. The initialization value of {1Mage2Pol 3} is 1Magne2Pol 3 in turn, its cap=len=3.
S: = arr [:]
Initialize slice s, which is a reference to the array arr.
S: = arr [startIndex:endIndex]
Create a new slice of the element in arr from subscript startIndex to endIndex-1.
S: = arr [startIndex:]
The default endIndex represents the last element up to arr.
S: = arr [: endIndex]
The default startIndex will start with the first element of the arr.
S1: = s [startIndex:endIndex]
Initialize slice S1 with slice s.
S: = make ([] int,len,cap)
The slice s is initialized with the built-in function make (), and [] int is identified as the slice whose element type is int.
This is the end of the content of "how to define and initialize Go language slices". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.