在Go中开发插件? [英] Develop plugins in Go?

查看:172
本文介绍了在Go中开发插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以动态运行以用于基于插件的应用程序吗?



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



同样的事情可能发生在Go吗?

我认为这些问题是两个不同的问题:具有动态负载的
$ b $ ol <

  • 具有插件的
  • li>

    第一个是简单否:Go程序是静态链接的,这意味着您不能将代码添加到正在运行的程序中。这也意味着您必须编译该程序,让它集成插件。



    幸运的是,您可以像在大多数语言中一样在Go中定义接受插件的程序,接口和快速编译并不会让这项任务变得困难。



    以下是两种可能的方法:

    解决方案1:集成在主程序中的插件类似于Eclipse插件,我们可以通过重新编译将插件集成到主程序存储器中该程序。从这个意义上说,我们可以说数据库驱动程序是插件。



    这可能不像在Java中那么简单,因为您必须重新编译,并且必须在某些您的代码点导入插件(请参阅​​它是如何完成数据库驱动程序的),但是,考虑到Go关于目录和导入的标准化,使用导入插件并重新编译应用程序的简单makefile来处理该问题似乎很简单。



    考虑到Go编译的简易性和速度,以及包结构的标准化,这似乎是一个非常可行的解决方案。



    <解决方案2:单独的过程


    Go在沟通和处理异步调用中特别容易。这意味着您可以基于多个通过命名管道(或任何网络解决方案)进行通信的进程来定义解决方案。请注意,Go中有一个 rpc软件包。这对大多数程序来说可能是有效的,主程序将能够启动和停止插件进程。这可能与您在Eclipse中使用内存空间保护带来的好处类似。



    有人编写了几个Eclipse插件:你不想那么混乱;保持简单。


    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 ?

    解决方案

    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.

    这篇关于在Go中开发插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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