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)06/03 Report--
Editor to share with you the example analysis of the slice structure in go, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Slice
The grammar of array: [4] int {1je 2je 3je 4}, [...] int {1je 2je 3}. In go, array is a value type, which means that a variable name of type array is not a pointer, and when a value is passed, array is always copied.
Syntax of slice: [] int {1 int 2 Jing 3 Jing 4}, make ([] int), make ([] int,10)
When make has only two parameters, cap and len are the same.
Slice is essentially a description of a fragment of array, which consists of three parts:
[ptr, len, cap]
The memory layout of the slice created by make ([] int, 5) is as follows:
After truncating the slice, a new slice is formed:
Slice can be amplified by cap: s [: cap (s)].
Usage of copy (dst, src [] T) int: copy replicates the number of elements with the smallest length in dst and src (so if dst==nil, copy returns 0). Also, copy can handle situations where dst and src overlap.
Usage:
T: = make ([] byte, len (s), (cap (s) + 1) * 2) copy (t, s) s = t
The implementation of append (dst [] T, element...T) [] T is similar to the following AppendByte:
First check the capacity, then use make to construct a new slice and move the original elements.
Func AppendByte (slice [] byte, data... byte) [] byte {m: = len (slice) n: = m + len (data) if n > cap (slice) {/ / if necessary, reallocate / / allocate double what's needed, for future growth. NewSlice: = make ([] byte, (Nation1) * 2) copy (newSlice, slice) slice = newSlice} slice = slice [0byte] copy (slice [MGV n], data) return slice} slice effect on gc (gotcha)
If you slice a very small piece of a large array, it will cause the array not to be recycled by gc.
Gc uses mark-and-sweep (tag cleanup). Gc maintains a table of allocated heap objects. In the marking phase, it traverses registers and pointers on the stack as root.
Why does part of the slice cause the overall array not to be recycled? Consider the following slice:
A: = [4] int {0pm 1jre 2je 3} s: = a [1:2] / / {1} return s
Will a be recycled? The answer is no, because when gc traverses s, it finds the corresponding array slice through the data pointer, and it finds that the address is within the range of a previously assigned array object, thus marking the array as reachable and preventing it from being cleared altogether. What to understand here is that array is marked by range, not by pointer header, because a memory block object is scoped, and if partially referenced, the whole object is still reachable. )
How to solve? If this is the case, a larger array returns smaller slices, and replication can be used:
Before:
Var digitRegexp = regexp.MustCompile ("[0-9] +") func FindDigits (filename string) [] byte {b, _: = ioutil.ReadFile (filename) return digitRegexp.Find (b)}
After:
Func CopyDigits (filename string) [] byte {b, _: = ioutil.ReadFile (filename) b = digitRegexp.Find (b) c: = make ([] byte, len (b)) copy (c, b) return c} reflect.SliceHeaderpackage reflect// SliceHeader and StringHeader have the same Data and Len, which results in the direct conversion of [] byte to string. Without any need to copy type SliceHeader struct {Data uintptr Len int Cap int} type StringHeader struct {Data uintptr Len int}
Conversion code:
Func SliceByteAsString (b [] byte) string {return * (* string) (unsafe.Pointer & b)} func StringAsSliceByte (s string) [] byte {p: = unsafe.Pointer (& d) var b [] byte hdr: = (* reflect.SliceHeader) (unsafe.Pointer (& b)) hdr.Data = uintptr (p) hdr.Cap = len (s) hdr.Len = len (s) return b} func Int64AsByteSlice (d int64) [] byte {p: = unsafe.Pointer (& d) var b [] byte hdr: = (* reflect.SliceHeader) (unsafe.Pointer (& b)) hdr.Data = uintptr (p) hdr.Cap = 8 hdr.Len = 8 return b} above are all the contents of the article "sample Analysis of slice structures in go" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.