登录
首页 >  Golang >  Go问答

Go插件 - "插件构建使用不同版本的包"

来源:stackoverflow

时间:2024-03-27 21:54:28 282浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《Go插件 - "插件构建使用不同版本的包"》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

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

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

我正在使用 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 已为最好奇的人保留。

终于介绍完啦!小伙伴们,这篇关于《Go插件 - "插件构建使用不同版本的包"》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>