[ +crawshaw ] On Thu, Nov 3, 2016 at 12:51 AM, Mateusz Dymiński <dymin...@gmail.com> wrote: > Hi, > > I am trying to load dynamically implementation of particular interface. I've > created the example in following repo: > https://github.com/mateuszdyminski/go-plugin > > I have interface - let's call it Printer: > package printer > > type Printer interface { > Print(text string) > } > > > And implementation of this interface: > package main > > import ( > "fmt" > > "github.com/mateuszdyminski/go-plugin/printer" > ) > > func main() { > Impl.Print("test") > } > > type PrinterImpl struct{} > > func (p PrinterImpl) Print(text string) { > fmt.Printf("[PrinterImpl] %s\n", text) > } > > var Impl printer.Printer = PrinterImpl{} > > > Then I can build that implementation of Printer as following: > go build -buildmode=plugin printer.go > > > The last step is to load dynamically created library 'printer.so': > package main > > import ( > "fmt" > "plugin" > "reflect" > > "github.com/mateuszdyminski/go-plugin/printer" > ) > > func main() { > lib, err := plugin.Open("printer.so") > if err != nil { > panic(err) > } > > p, err := lib.Lookup("Impl") > if err != nil { > panic(err) > } > > printerImpl, ok := p.(printer.Printer) > if !ok { > fmt.Printf("wrong type: %+v \n", reflect.TypeOf(p)) > panic("wrong type") > } > > printerImpl.Print("test") > } > > > But when I run it with 'go run main.go' I got following error: > wrong type: *printer.Printer > panic: wrong type > > goroutine 1 [running]: > panic(0x50fda0, 0xc42000c2d0) > /home/md/.gvm/gos/master/src/runtime/panic.go:531 +0x1cf > main.main() > /home/md/workspace/go/src/github.com/mateuszdyminski/go-plugin/main.go:25 > +0x231 > exit status 2 > > > The type of the 'Impl' taken from 'reflect' package is '*printer.Printer'. > Is it possible to cast it to the printer.Printer and not to the pointer to > the interface? > > Thanks for any help! > > -- > 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.
-- 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.