In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.