Develop plugins in Go?

前端 未结 4 851
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 05:58

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 t

相关标签:
4条回答
  • 2020-12-29 06:21

    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".

    0 讨论(0)
  • 2020-12-29 06:22

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

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

    0 讨论(0)
  • 2020-12-29 06:29

    A few packages I found to do this:

    • https://golang.org/pkg/net/rpc/
    • https://github.com/hashicorp/go-plugin
    • https://github.com/natefinch/pie
    0 讨论(0)
  • 2020-12-29 06:40

    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.

    0 讨论(0)
提交回复
热议问题