Eric Raymond <e...@thyrsus.com> writes:
> Obviously we can't use inheritance in the normal sense here, but I was > hoping that if an embedded instance of Cmd could discover the struct > instance it's part of, a similar thing could be pulled off. Alas, your > answer that this can't be done is unsurprising and I was half expecting > it. Calling one function to pass in the outer type beats having to declare > all the command handlers individually.... There's no typename to reflected type or enumeration of all types that implement a particular interface at runtime, unless you build it yourself. You might be interested in the init() documentation at https://golang.org/doc/effective_go.html#init . You can declare multiple init functions (say one in each file where you define a command), and if you write the registration when you write the command, you would get the automatic registration you're looking for. A 'gotcha' to watch out for is that the init function won't run if the package isn't imported, leading to imports like this: import ( _ "github.com/someuser/somepackage" ) which are common for the database drivers. The package is imported solely to cause the init function to run. See https://golang.org/doc/effective_go.html#blank_import for more info. > ...provided, that is, that it's possible to get from the member name to a > function pointer that can be called like a closure. Does the reflection > system support such a call-indirect method? Sure, see an example I put together at https://play.golang.org/p/divEOT31i-4 which shows two ways that the receiver can be captured, one without reflection and one with. - Todd -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.