In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use unsafe in Go language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Introduction to pointer types:
For the pointer type of the Go language, there are many restrictions on the pointer type, which mainly has four points, which can be detected in the compilation phase of the GE language.
Limitation 1: pointers cannot be calculated mathematically.
Limitation 2: different types of pointers cannot be assigned to each other.
Limit 3: different types of pointers cannot be compared with = or!
Limitation 4: different types of pointers cannot be converted to each other.
Note: the Go language is so strict on types that even int and INT in type INT int do not think that they are the same type, which leads to more stringent restrictions on pointers.
1) examples of normal use of pointer variables:
2) examples where pointer variables are restricted:
2. Unsafe introduction
From the introduction of pointer types above, we can see that unsafe exists because the restrictions on pointer types in the Go language are too strict. The emergence of unsafe precisely makes up for these limitations of pointer variables, but from the point of view of the designers of the Go language, they actually do not want people to use the unsafe package, but some scenarios have to use it, so they specially named a package called unsafe to use it.
2.1 scenarios used by unsafe
Scenario 1: resolve the mismatch of the data types used by the pointer types provided by the third-party library.
Scenario 2: solve the value and modification of invisible attributes in the data structure.
2.2 Features of unsafe
Scenario 1: any type of pointer can be converted to unsafe.Pointer,unsafe.Pointer or any type of pointer, as shown in the following example:
From the above example, and the output, we can see that unsafe.Pointer can convert a pointer variable of type int into a pointer variable of type float32.
Scenario 2: uintptr can be converted to Pointer,Pointer or uintptr, as shown in the following example:
Result analysis: from the output of the above example, you can see how the unsafe package operates and modifies the invisible elements age and name in the data structure Node. When we modify the first element in Node, we only need to get the storage location of the data structure; when we modify the subsequent elements, we need to use unsafe.Offsetof to calculate the offset to find the storage location of the corresponding element, and then modify the data.
2.3 uintptr do not assign values to temporary variables
From the perspective of GC, a temporary variable of type uintptr is only an unsigned integer, and it is not known that it is a pointer address, so when certain conditions are met, the temporary variable ptr may be garbage collected. Therefore, we cannot assign uintptr (nP) to a temporary variable first and perform the following address fetch operation. The code is as follows:
This is how unsafe is used in the Goe language shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.