Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to pass parameters through the interface in golang?
How to pass parameters through the interface in golang?
Golang's interface is different from other languages. It doesn't need an explicit implementation. As long as a structure implements all the functions in the interface, the compiler will automatically assume that it implements the interface.

SICP explains in detail why the same interface needs different implementations according to different data types. And how to do it. There is no concept of OO here. Let's put OO aside and see how it is done in principle.

Let's put the general principle here first and then give an example. In order to realize polymorphism, it is necessary to maintain a global lookup table, whose function is to return corresponding function entries according to type names and method names. When I add a type, I need to add the name of the new type, the corresponding method name and the actual function entry to the table. This is basically the so-called dynamic binding, similar to vtable in C++. For the lisp language used by SICP, these tasks need to be completed manually. For java, this work is done through implementation. On the other hand, Golang took a more radical approach, even omitting the implementation, and the compiler automatically found the automatic binding.