Reputation: 3855
Is it possible to get a slice of strings that represent the names of all types that implement an interface or inherit from a specific struct in a specific package using reflection?
Upvotes: 3
Views: 351
Reputation: 58231
The go oracle can do this. https://godoc.org/code.google.com/p/go.tools/oracle
Here is the relevant section of the user manual.
Upvotes: 2
Reputation: 36199
AFAIK, you can't do this with reflect
, since packages are kinda out of reflect
's scope.
You can do this the same way godoc's static analysis works. That is, using code.google.com/p/go.tools/go/types
to parse the package's source code and get the type info.
Upvotes: 2
Reputation: 9509
After some research on the reflect
package's doc, I don't think it's possible. That's not the way reflection work in go: the interfaces mechanism not beeing declarative (but duck-typed instead), there is no such list of types.
That said, you may have more luck using the ast
package to parse your project, get the list of types, and check wheter or not they implement an interface, then write some code to give you the said slice. That would add a step to compilation, but could work like a charm.
Upvotes: 4