In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is mainly about "how to solve the pit of Go interface". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to solve the pit of Go interface"!
Example one
For the first example, the code is as follows:
Func main () {var v interface {} v = (* int) (nil) fmt.Println (v = = nil)}
What do you think the output is?
The answer is:
False
Why not true. Obviously, it has been forcibly placed as nil. Is there a problem with the Go compiler?
Example two
For the second example, the following code:
Func main () {var data * byte var in interface {} fmt.Println (data, data = = nil) fmt.Println (in, in = = nil) in = data fmt.Println (in, in = = nil)}
What do you think the output is?
The answer is:
True true false
This is even more strange, why the data and in variables just declared, the output is indeed nil, the judgment result is also true.
How can the world change as soon as the variable data is assigned to the variable in? The output is still nil, but the decision becomes false.
The result is similar to the first example above, which is amazing.
Reason
The fundamental reason why interface's judgment is different from what he imagined is that interface is not a pointer type, although it looks so similar that it misleads many people.
We drill down into interface,interface and there are two types of data structures:
Runtime.eface structure: represents an empty interface that does not contain any methods, also known as empty interface.
Runtime.iface structure: represents the interface that contains the method.
Take a look at the underlying data structures corresponding to the two:
Type eface struct {_ type * _ type data unsafe.Pointer} type iface struct {tab * itab data unsafe.Pointer}
You will find that interface is not a simple value, but is divided into types and values.
Therefore, the traditional cognition of this nil is not that nil, and the nil judgment of interface will be true only if the type and value are both nil at the same time.
Solution.
It is not so much a solution as an euphemistic way to break the situation. One way to do this without changing the type is to use reflect, as shown in the following code:
Func main () {var data * byte var in interface {} in = data fmt.Println (IsNil (in))} func IsNil (I interface {}) bool {vi: = reflect.ValueOf (I) if vi.Kind () = reflect.Ptr {return vi.IsNil ()} return false}
Using reflection to judge the value of nil, there will be special processing for interface type in reflection, and the final output result is: true, to achieve the effect.
Other ways are to change the original program logic, such as:
The value is judged by nil and returned to the interface setting.
Returns the specific value type instead of interface.
At this point, I believe you have a deeper understanding of "how to solve the pit of Go interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.