I know that it is possible to look up for go-plugin symbols that were exported and type assert them into an interface. However, I wonder if is there a way to type assert them into a struct, for example. Is there a way to do it?
For example:
plugin.go
package main
type Person struct {
Name string
}
var (
P = Person{
Name: "Emma",
}
)
app.go
package main
import (
"fmt"
"plugin"
"os"
)
func main() {
plug, err := plugin.Open("./plugin.so")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sym, err := plug.Lookup("P")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var p Person
p, ok := sym.(Person)
if !ok {
fmt.Println("Wrong symbol type")
os.Exit(1)
}
fmt.Println(p.Name)
}
The symbol P, which is a Person, is found when plug.Lookup is called. However, I can't type-assert P into Person, I get an execution time error. In this example, "Wrong symbol type".
Is there a way to achieve this or the only way to share data between the plugin and the application is using interfaces?
Thanks.
Identifiers defined in the
main
package cannot be referred to from other packages, so an exported identifier from a plugin cannot be the same type what you have in your main app. Even if you would duplicate thePerson
type both in the plugin's file and in your main app, type assertion would fail because they are not the same type!But it is possible to define the type in a separate package, and use this same package in the plugin and in the main app. And then you can type assert this type from a symbol you lookup from the plugin.
See this example:
The separate type defined in its own package:
Plugin code:
The main app:
Running the main app, the output is:
What you may notice is that the exported identifier in the plugin
MyFilter
is a variable of non-pointer type, yet we type-asserted a pointer type from the exported symbol. This is because if you lookup a variable, you will get a pointer to it, else you could not modify the value of the variable, you could only modify the copy. This is detailed in this answer: Plugin symbol as function returnThis is not the case if we lookup the other symbol our plugin exports: the
CreateFilter()
function which returns a value of non-pointer typefilter.Filter
:Running this code, the output will be:
See related question: go 1.8 plugin use custom interface
Also note that if you change the
filter
package used commonly by the plugin and the main app, you also have to rebuild the plugin. Attempting to run the app without rebuilding the plugin will result in an error during theplugin.Open()
call. For details, see How do Go plugin dependencies work?