Go 插件 - "plugin was built with a different version of package"

Go plugin - "plugin was built with a different version of package"

我有一个在启动时加载插件的应用程序(守护进程)。在子包 (daemon/interfaces) 中,我有一些接口供此程序的插件使用。

这意味着主程序也被插件导入。

我正在使用 Go 模块(用于主程序和插件)来修复版本,我可以在 go.mod 中看到它正在为插件使用最新版本的主程序。

我可以很好地构建它们,但是当我加载插件时它给我一个错误提示

 panic: plugin.Open("plugins/my-plugin"): plugin was built with a different version of package daemon/interfaces

我正在使用 Go 1.12.7 构建这两个包。

我通过在我的插件 go.mod 文件中添加一个替换语句来解决这个问题

module github.com/user/plugin

go 1.12

require (
    github.com/user/daemon v1.1.1
)

replace github.com/user/daemon v1.1.1 => ../local/path/to/daemon/

当您在源代码所在的目录之外使用项目的全名构建项目时,它也会有所帮助 (go build github.com/user/project/)

Golang 存储库中有一个相关的 Github 问题 that you can find here

显然,the issue 还在营业。问题开启者提出了我能够使用的解决方法。请查看下面的历史行以了解详细信息。

git clone https://github.com/zimnx/central.git
git clone https://github.com/zimnx/plugins.git
cd central/
go clean -modcache
git checkout v1.0.0
go install -a
cd ../plugins/
rm go.mod 
go mod init github.com/zimnx/plugins
echo '' >> go.mod
echo 'replace github.com/zimnx/central => ../central' >> go.mod
go build -buildmode=plugin -o plugin.so
central plugin.so 

适合我。仍然是谜... :) The output 已保存给最好奇的人。