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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀