In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what is the relationship between Go language and duck type. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The relationship between Go language and duck types
Let's take a look directly at the definition in Wikipedia:
If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.
If something looks like a duck, swims like a duck and quacks like a duck, it can be regarded as a duck.
DuckTyping, the duck type, is an object inference strategy for dynamic programming languages that focuses more on how objects can be used than on the type of objects themselves. As a static language, Go perfectly supports the duck type through interfaces.
When calling this function, you can pass in any type, as long as it implements the say_hello () function. If it is not implemented, an error will occur during the run.
In static languages such as Java,C++, an interface must be explicitly declared before it can be used wherever it is needed. If you call the hello_world function in your program and pass in a type that doesn't implement say_hello () at all, it won't pass at compile time. This is why static languages are more secure than dynamic languages.
The difference between dynamic language and static language is reflected here. Static languages can find type mismatch errors during compilation, unlike dynamic languages, which have to run to that line of code to report an error. By the way, this is one of the reasons why I don't like using python. Of course, static languages require programmers to write programs in accordance with the rules during the coding phase, specifying data types for each variable, which, to some extent, increases the workload and the amount of code. Dynamic languages do not have these requirements and can make people more focused on business, shorter code and faster writing, which is clear to students who write python.
As a modern static language, Go has the advantage of backwardness. It introduces the convenience of dynamic languages while at the same time checking the type of static languages, which is very Happy to write. Go takes a compromise: it does not require the type to declare that an interface is implemented, but as long as the relevant methods are implemented, the compiler can detect it.
Program output:
In the main function, when you call the sayHello () function, you pass in the golang,php object, which does not explicitly declare that the IGreeting type is implemented, but only implements the sayHello () function specified by the interface. In fact, the compiler implicitly converts golang,php objects to IGreeting types when calling the sayHello () function, which is also a type-checking feature of static languages.
Incidentally, I would like to mention the features of dynamic languages:
The type of variable binding is uncertain, it can only be determined at run time that functions and methods can receive any type of parameters, and the call does not check the parameter type and does not need to implement the interface
To sum up, the duck type is a dynamic language style in which the valid semantics of an object is determined not by inheriting from a particular class or implementing a specific interface, but by its "current collection of methods and properties". As a static language, Go implements the duck type through the interface. In fact, the compiler of Go has done hidden conversion work in it.
So much for sharing about the relationship between Go language and duck types. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
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: 264
*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.