I started to play with this new plugin feature and I got some problems when
I tried to load two different plugins one by one.

If I have first plugin as it was describe in the original post and new one:

*Interface:*

package processor

type Processor interface {
Process(text string)
}

*And implementation:*

package main

import (
"fmt"

"github.com/mateuszdyminski/go-plugin/processor"
)

func main() {
Impl.Process("test")
}

type ProcessorImpl struct{}

func (p ProcessorImpl) Process(text string) {
fmt.Printf("[ProcessorImpl] %s\n", text)
}

var Impl processor.Processor = ProcessorImpl{}

*And the main.go file where I am trying to load the both plugins:*

package main

import (
"plugin"
"github.com/mateuszdyminski/go-plugin/printer"
"github.com/mateuszdyminski/go-plugin/processor"
)

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 {
panic("wrong type")
}

(*printerImpl).Print("test")

lib2, err := plugin.Open("processor.so")
if err != nil {
panic(err)
}

p2, err := lib2.Lookup("Impl")
if err != nil {
panic(err)
}

processorImpl, ok := p2.(*processor.Processor)
if !ok {
panic("wrong type")
}

(*processorImpl).Process("test")
}


*I got following error: *

md@md go-plugin (master)$ go run main.go
[PrinterImpl] test
plugin: plugin command-line-arguments already loaded
fatal error: plugin: plugin already loaded

goroutine 1 [running]:
runtime.throw(0x528f5e, 0x1d)
/home/md/.gvm/gos/master/src/runtime/panic.go:596 +0x95 fp=0xc420057b48
sp=0xc420057b28
plugin.lastmoduleinit(0x14d7400, 0xc42000f848, 0x14d8ba0)
/home/md/.gvm/gos/master/src/runtime/plugin.go:25 +0xa69 fp=0xc420057c20
sp=0xc420057b48
plugin.open(0x52607d, 0xc, 0x0, 0x0, 0x0)
/home/md/.gvm/gos/master/src/plugin/plugin_dlopen.go:72 +0x25d
fp=0xc420057e40 sp=0xc420057c20
plugin.Open(0x52607d, 0xc, 0x4, 0x4, 0xc4200a8088)
/home/md/.gvm/gos/master/src/plugin/plugin.go:30 +0x35 fp=0xc420057e78
sp=0xc420057e40
main.main()
/home/md/workspace/go/src/github.com/mateuszdyminski/go-plugin/main.go:31
+0x242 fp=0xc420057f88 sp=0xc420057e78
runtime.main()
/home/md/.gvm/gos/master/src/runtime/proc.go:185 +0x20a fp=0xc420057fe0
sp=0xc420057f88
runtime.goexit()
/home/md/.gvm/gos/master/src/runtime/asm_amd64.s:2184 +0x1 fp=0xc420057fe8
sp=0xc420057fe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/home/md/.gvm/gos/master/src/runtime/asm_amd64.s:2184 +0x1
exit status 2


*Summary:*
The true is that I got this error: "plugin: plugin command-line-arguments
already loaded" every time I try to load the plugin twice. Is this still
unfinished feature and I just should wait for the further development? Or I
should create the issue for that on the github? Or it's proper behavior of
the plugin functionality?

If anyone would like to check the issue - code is available on the
https://github.com/mateuszdyminski/go-plugin


2016-11-03 15:11 GMT+01:00 Mateusz Dymiński <dymin...@gmail.com>:

> Ahhhh, thanks a lot for your help and sorry for trivial question.
>
>
> To test the type assertion I should use following code:
>
>
> printerImpl, ok := p.(*printer.Printer)
> if !ok {
> panic("wrong type")
> }
>
> (*printerImpl).Print("test")
>
>
> I was trying to call the 'Print' func as *printerImpl.Print("test") not
> (*printerImpl).Print("test").
>
>
> Thanks again!
>
>
>
> 2016-11-03 14:51 GMT+01:00 David Crawshaw <craws...@golang.org>:
>
>> The Lookup method is returning a pointer to the symbol (That way the
>> program can modify the value of Impl through the plugin.)
>>
>> I believe you can write:
>>
>> printerImpl := *p.(*printer.Printer)
>>
>> On Thu, Nov 3, 2016 at 9:44 AM, Ian Lance Taylor <i...@golang.org> wrote:
>> > [ +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-plug
>> in/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.

Reply via email to