用Go语言开发插件?

huangapple go评论78阅读模式
英文:

Develop plugins in Go?

问题

能否在Go中动态运行以用于基于插件的应用程序?

在Eclipse中,我们可以创建一些插件,Eclipse可以动态运行这些插件。

在Go中是否也可能实现相同的功能?

英文:

Can go run dynamically in order to be used for a plugin based application ?

In eclipse, we can create some plugins that Eclipse can run dynamically.

Would the same thing be possible in Go ?

答案1

得分: 22

我会争辩说这是两个不同的问题:

  1. 动态加载
  2. 插件

第一个问题很简单:Go程序是静态链接的,这意味着您无法向正在运行的程序中添加代码。这也意味着您必须编译程序以使其集成插件。

幸运的是,您可以像大多数语言一样在Go中定义接受插件的程序,并且借助接口和快速编译,Go并不难实现这个任务。

以下是两种可能的方法:

解决方案1:将插件集成到主程序中

类似于Eclipse插件,我们可以通过重新编译程序将“插件”集成到主程序内存中。在这个意义上,我们可以说数据库驱动程序是插件。

这可能不像Java那样简单,因为您必须重新编译,并且在代码的某个点上导入“插件”(参见如何为数据库驱动程序完成此操作),但是鉴于Go在目录和导入方面的标准化,使用一个简单的makefile导入插件并重新编译应用程序似乎很容易处理。

鉴于Go的编译的简便和速度,以及包结构的标准化,这对我来说似乎是一个非常可行的解决方案。

解决方案2:独立进程

在Go中,通信和处理异步调用特别容易。这意味着您可以基于多个进程通过命名管道(或任何网络解决方案)定义解决方案。请注意,Go中有一个rpc包。对于大多数程序来说,这可能已经足够高效了,而且主程序将能够启动和停止插件进程。这可能会让您感觉与Eclipse中的情况非常相似,并且还具有内存空间保护的附加好处。

一个写过多个Eclipse插件的人的最后一点建议:您不希望出现混乱;保持简单。

英文:

I'll argue that those are two separate problems :

  1. having dynamic load
  2. having plugins

The first one is simply no : A Go program is statically linked, which means you can't add code to a running program. And which also means you must compile the program to let it integrate plugins.

Fortunately, you can define a program accepting plugins in Go as in most languages, and Go, with interfaces and fast compilation doesn't make that task hard.

Here are two possible approaches :

Solution 1 : Plugin integrated in the main program

Similarly to Eclipse plugins, we can integrate the "plugins" in the main program memory, by simply recompiling the program. In this sense we can for example say that database drivers are plugins.

This may not feel as simple as in Java, as you must have a recompilation and you must in some point of your code import the "plugin" (see how it's done for database drivers) but, given the standardization of Go regarding directories and imports, it seems easy to handle that with a simple makefile importing the plugin and recompiling the application.

Given the ease and speed of compilation in Go, and the standardization of package structure, this seems to me to be a very viable solution.

Solution 2 : Separate process

It's especially easy in Go to communicate and to handle asynchronous calls. Which means you could define a solution based on many process communicating by named pipes (or any networking solution). Note that there is a rpc package in Go. This would probably be efficient enough for most programs and the main program would be able to start and stop the plugin processes. This could very well feel similar to what you have in Eclipse with the added benefits of memory space protection.

A last note from somebody who wrote several Eclipse plugins : you don't want that mess; keep it simple.

答案2

得分: 4

Go 1.8支持插件(将于2017年2月发布)。

英文:

Go 1.8 supports plugins (to be released soon Feb 2017.)

https://tip.golang.org/pkg/plugin/

答案3

得分: 1

正如dystroy已经说过的,无法在运行时加载包。

在未来(或者在有限制的情况下,今天)可能可以通过像go-eval这样的项目实现此功能,它是“Go的解释器的开始”。

英文:

As dystroy already said, it's not possible to load packages at runtime.

In the future (or today with limitations) it may be possible to have this feature with projects like go-eval, which is "the beginning of an interpreter for Go".

答案4

得分: 0

这是我找到的一些用于实现此功能的包:

英文:

A few packages I found to do this:

huangapple
  • 本文由 发表于 2012年9月16日 10:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/12444022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定