duanjiancong4860 2017-05-15 11:18
浏览 37
已采纳

Golang 1.8插件。查找仅加载第一个插件

I'm writing a Rules Engine in Golang. My tests involve building and loading two plugins that do some very simple string comparison. I load the plugins by doing a fileglob and calling plugin.Open(), so far so good we get two different objects back. But when I call plug.Lookup(symbol) the symbol returned is always a reference to the first plugin.

DEBU[0000] Starting A rule

DEBU[0000] Starting A rule

I've debugged back to make sure the plugins are different objects, which they are, but the symbol lookup isn't working as expected.

Here's a contrived working example:

main.go package main

import "plugin"
import "fmt"

func main() {
    aPlug, _ := plugin.Open("testdata/plugins/a.so")
    aSymPlug, _ := aPlug.Lookup("Rule")
    fmt.Printf("Plugin: %v loaded
", aSymPlug)

    bPlug, _ := plugin.Open("testdata/plugins/b.so")
    bSymPlug, _ := bPlug.Lookup("Rule")
    fmt.Printf("Plugin: %v loaded
", bSymPlug)
}

a.go plugin

package main

type plugin string

func init() {
    Rule = "a"
}

func (p plugin) String() string {
    return string(p)
}

var Rule plugin

b.go plugin

package main

type plugin string

func init() {
    Rule = "b"
}

func (p plugin) String() string {
        return string(p)
}

var Rule plugin

BUT when I change the plugin to return the string "a" or "b" (doesn't matter which plugin, either or both) instead of p then it loads the wrong plugin! i.e.

package main

type plugin string

func init() {
    Rule = "b"
}

func (p plugin) String() string {
        return "b"
}

var Rule plugin

This outputs:

Plugin: a loaded
Plugin: a loaded

EDIT: Added example code

  • 写回答

1条回答 默认 最新

  • doujiayao8433 2017-05-18 07:36
    关注

    This is a Golang bug (see https://github.com/golang/go/issues/20376)

    The workaround, as pointed out by Motakjuq, is to change the name of the type so each plugin has a different type. This avoids the collision.

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

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程