Home > Backend Development > Golang > Gets the name of a structure that implements an interface or inherited structure

Gets the name of a structure that implements an interface or inherited structure

WBOY
Release: 2024-02-10 13:42:09
forward
1146 people have browsed it

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.

Question content

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?

Workaround

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.

That said, you might have better luck using the 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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template