Get the names of structures that implement an interface, or inherit a structure

Is it possible to get a snippet of strings that represent names of all types that implement an interface or inherit from a specific structure in a specific package using reflection?

+3


source to share


3 answers


After reflect

doing some research on the docs , I don't think this is possible. This is not how go is reflected: the interface mechanism is not declarative (but instead of duck), there is no such list.



However, you might be in luck with a package ast

to parse your project, get a list of types and check for the presence or absence of an interface, then write some code to give you the snippet specified. This will add a step to compilation, but can work like a charm.

+4


source


AFAIK, you cannot do this with reflect

, since the packages look out of scope reflect

.



You can do it the same way Godoc static analysis works. That is, using code.google.com/p/go.tools/go/types

to parse the source code of the package and get the type information.

+2


source


+2


source







All Articles