Home >Backend Development >Golang >Gets the name of a structure that implements an interface or inherited structure
php editor Youzi introduces a method to get the structure name of the implementation interface or inheritance structure. In programming, we often need to obtain which interfaces a class implements or which structures it inherits. By using reflection mechanism we can easily obtain this information. Just use the getInterfaceNames() method and getParentClass() method of the ReflectionClass class, we can get the names of the interfaces implemented by the class and the names of the inherited structures. This method is simple and practical, and can help us better understand and analyze the code structure.
Is it possible to use reflection to get a slice of string representing the names of all types that implement an interface or inherit from a specific structure in a specific package?
After doing some research on the documentation for the reflect
package, I don't think this is possible. This is not how reflection works in Go: the interface mechanism is not declarative (but duck-typed), and there is no such list of types.
ast
package to parse your project, get a list of types, and check if they implement the interface, and then write some code to give you the above slice. This adds a compilation step, but the results are great. The above is the detailed content of Gets the name of a structure that implements an interface or inherited structure. For more information, please follow other related articles on the PHP Chinese website!