In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you whether the Gog language compiler automatically detects whether the type implements the interface. The content is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The compiler automatically detects whether the type implements the interface
It is often seen that some open source libraries have strange uses similar to the following:
At this time will be a little confused, do not know what the author wants to do, in fact, this is the answer to this question. The compiler checks to see if the * myWriter type implements the io.Writer interface.
In fact, the above assignment statement undergoes an implicit type conversion, during which the compiler detects whether the type to the right of the equal sign implements the function specified by the interface to the left of the equal sign.
What is the construction process of the interface?
We have looked at the source code of iface and eface and know that the most important things for iface are itab and _ type.
In order to understand how the interface is constructed, I will then pick up the assembly weapon and restore the truth behind it.
Add up the size of each field and the size of the itab structure is 40 bytes. The above string of numbers is actually serialized by itab, notice that most of the numbers are 0, and the 4-byte da9f20d4 starting from 24 bytes is actually the hash value of itab, which is used to determine whether the two types are the same.
The following two lines are link instructions, which simply combine all the source files and assign each symbol a global location value. The meaning here is also clear: the first 8 bytes end up storing the address of type. ". Person, corresponding to the inter field in itab, indicating the interface type; 8-16 bytes finally storing the address of type.". Student, corresponding to the _ type field in itab, indicating the specific type.
The second parameter is relatively simple, which is the address of the number 18, which is also used when initializing the Student structure.
In the main function, first construct an interface object, qcrao, then cast, and finally read out the hash value, which is very good! You can also try it yourself.
The above content is that the Go language compiler automatically detects whether the type implements the interface. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.