dongtieshang5429 2018-05-05 23:21
浏览 74
已采纳

是否可以在go插件和应用程序之间共享自定义数据类型?

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.

  • 写回答

1条回答 默认 最新

  • drui0508 2018-05-23 18:35
    关注

    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 the Person 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:

    package filter
    
    type Filter struct {
        Name string
        Age  int
    }
    

    Plugin code:

    package main
    
    import (
        "play/filter"
    )
    
    var MyFilter = filter.Filter{
        Name: "Bob",
        Age:  21,
    }
    
    func CreateFilter() filter.Filter {
        return filter.Filter{
            Name: "Bob",
            Age:  21,
        }
    }
    

    The main app:

    package main
    
    import (
        "fmt"
        "log"
        "os"
        "play/filter"
        "plugin"
    )
    
    func main() {
        p, err := plugin.Open("plugin.so")
        if err != nil {
            log.Fatal(err)
        }
        mf, err := p.Lookup("MyFilter")
        if err != nil {
            log.Fatal(err)
        }
        f, ok := mf.(*filter.Filter)
        if !ok {
            log.Fatal("Wrong symbol type")
        }
    
        fmt.Printf("%+v
    ", f)
    }
    

    Running the main app, the output is:

    &{Name:Bob Age:21}
    

    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 return

    This is not the case if we lookup the other symbol our plugin exports: the CreateFilter() function which returns a value of non-pointer type filter.Filter:

    cf, err := p.Lookup("CreateFilter")
    if err != nil {
        log.Fatal(err)
    }
    
    createFilter, ok := cf.(func() filter.Filter)
    if !ok {
        log.Fatal("Wrong function type")
    }
    f2 := createFilter()
    fmt.Printf("%+v
    ", f2)
    

    Running this code, the output will be:

    {Name:Bob Age:21}
    

    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 the plugin.Open() call. For details, see How do Go plugin dependencies work?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误