在golang中动态加载包 [英] Load package dynamically in golang

查看:953
本文介绍了在golang中动态加载包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时加载特定的包?
我想要一种插件,其中每个插件具有与其他功能相同的功能,但具有不同的行为,并且根据配置文件加载一个或另一个。
谢谢。

Is it possible to load a specific package during runtime? I want to have a kind of plugins where each one has the same functions than the others but with different behaviour, and depending on the configuration file, load one or other. Thanks.

推荐答案

您可以考虑在运行时执行plugin包,方法是写出一个新的程序),并通过 exec.Command 执行,沿着 exec.Command(go ,运行,文件...).Run()

You might consider executing the ‘plugin’ packages at runtime, by writing out a new program (say, to a temp directory) and executing via exec.Command, something along the lines of exec.Command("go", "run", files…).Run()

你会看到一些类似的代码这里

You’ll see some similar code here.

这篇关于在golang中动态加载包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆