4

I have the following code in a golang plugin module:

plug.go

package main

import "fmt"

var (
    Thing        = New("first thing")
    ThingFactory = thingFactory{}
)

type thing struct {
    i int
    s string
}

func New(s string) thing {
    return thing{s: s}
}

func (t *thing) Say() string {
    t.i++
    return fmt.Sprintf("%s - %d", t.s, t.i)
}

type thingFactory struct{}

func (t thingFactory) Make(s string) thing {
    return New(s)
}

it is compiled as a .so object and used in another program:

main.go

package main

import (
    "fmt"
    "plugin"
)

func main() {
    p, err := plugin.Open("../plug/plug.so")
    if err != nil {
        panic(err)
    }
    symbol, err := p.Lookup("Thing")
    if err != nil {
        panic(err)
    }
    thing := symbol.(Sayer)
    fmt.Println(thing.Say())

    symbol, err = p.Lookup("ThingFactory") // <-problems start here
    if err != nil {
        panic(err)
    }
    factory := symbol.(GetSayer)

    madeThing := factory.Make("how about me?")
    fmt.Println(madeThing.Say())
    fmt.Println(madeThing.Say())
}

type Sayer interface {
    Say() string
}

type GetSayer interface {
    Make(string) Sayer
}

I'm able to lookup the Thing, and call Say() on it, but the second interface conversion panics:

first thing - 1 panic: interface conversion: *main.thingFactory is not main.GetSayer: missing method Make

even though the runtime recognizes the first symbol as a Sayer it doesn't recognize that thingFactory obviously has a Make() method, which should return something that is also a Sayer.

Am I missing something obvious here?

domoarigato
  • 2,802
  • 4
  • 24
  • 41

2 Answers2

3

The first problem is that in your plugin thingFactory (more precicely *thingfactory) does not have a method described in your main app's GetSayer interface:

Make(string) Sayer

You have:

Make(string) thing

So (first) you have to change thingFactory.Make() to this:

type Sayer interface {
    Say() string
}

func (t thingFactory) Make(s string) Sayer {
    th := New(s)
    return &th
}

After this it still won't work. And the reason for this is because the plugin's Sayer type is not identical to your main app's Sayer type. But they must be the same in order to implement your main app's GetSayer interface.

One solution is to "outsource" the Sayer interface to its own package, and use this common, shared package both in the plugin and in the main app.

Let's create a new package, call it subplay:

package subplay

type Sayer interface {
    Say() string
}

Import this package and use it in the plugin:

package main

import (
    "fmt"
    "path/to/subplay"
)

var (
    Thing        = New("first thing")
    ThingFactory = thingFactory{}
)

type thing struct {
    i int
    s string
}

func New(s string) thing {
    return thing{s: s}
}

func (t *thing) Say() string {
    t.i++
    return fmt.Sprintf("%s - %d", t.s, t.i)
}

type thingFactory struct{}

func (t thingFactory) Make(s string) subplay.Sayer {
    th := New(s)
    return &th
}

And also import and use it in the main app:

package main

import (
    "fmt"
    "path/to/subplay"
    "plugin"
)

func main() {
    p, err := plugin.Open("../plug/plug.so")
    if err != nil {
        panic(err)
    }
    symbol, err := p.Lookup("Thing")
    if err != nil {
        panic(err)
    }
    thing := symbol.(subplay.Sayer)
    fmt.Println(thing.Say())

    symbol, err = p.Lookup("ThingFactory")
    if err != nil {
        panic(err)
    }
    factory := symbol.(GetSayer)

    madeThing := factory.Make("how about me?")
    fmt.Println(madeThing.Say())
    fmt.Println(madeThing.Say())
}

type GetSayer interface {
    Make(string) subplay.Sayer
}

Now it will work, and output will be:

first thing - 1
how about me? - 1
how about me? - 2

See related questions:

go 1.8 plugin use custom interface

How do Go plugin dependencies work?

icza
  • 389,944
  • 63
  • 907
  • 827
  • thanks - I was sure it _should_ be possible, but the third package "outsourcing" was non obvious. I did some searching, but unfortunately there still isn't a ton of plugin use in the wild. – domoarigato Dec 23 '18 at 21:11
1

Your plugin Make method should return a Sayer object not thing

type Sayer interface {
    Say() string
}
func (t *thingFactory) Make(s string) Sayer {
    return New(s)
}
Mostafa Solati
  • 1,235
  • 2
  • 13
  • 33
  • I didn't assert it was `Sayer` - I asserted `GetSayer` - the factory type. The interface you defined as `Factory` is identical to the interface I defined, only I called it `GetSayer`. Also, putting another interface in the plugin code, means that the interface conversion results in pointer to interface problems. – domoarigato Dec 23 '18 at 16:52
  • 1
    @domoarigato the signature of GetSayer and plugin's Make method are different. Change Plugin method to return Sayer – Mostafa Solati Dec 23 '18 at 17:08
  • That's the thing, they actually aren't different. `thingFactory` has a `Make` method, which returns a `thing`, which the compiler recognizes is a `Sayer`, therefore `thingFactory` implements `GetSayer`. The `Sayer` interface is in the host code, not the plugin Code, and putting a different interface, which also happens to be called `Sayer` in the plugin code, and returning it doesn't change anything about the situation (I did try it) except that it does cause issues with *interface in the type conversion. – domoarigato Dec 23 '18 at 17:10
  • Tried it, sorry, but it doesn't work. It still doesn't recognize that plugin.thingFactory is host.GetSayer: missing method Make - same problem. – domoarigato Dec 23 '18 at 17:38