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
go


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 to share


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 to share


The Oracle can do it. https://godoc.org/code.google.com/p/go.tools/oracle

Here is the relevant section of the user manual.

+2


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics