22

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 ?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
vvilp
  • 423
  • 4
  • 10

4 Answers4

22

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.

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    [An implementation of the solution 2](https://github.com/mitchellh/packer/issues/1) – baijum Jun 14 '14 at 01:54
  • After having a look at pie, I am thinking of achieving a plugin structure using grpc instead of rpc - any thoughts on that? I'm a bit worried of the overhead as I might have to transfer some quite big amounts of data between the plugins (still thinking about how exactly I will implement all this) – Christoph Sonntag Oct 02 '16 at 16:15
4

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

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

weima
  • 4,653
  • 6
  • 34
  • 55
1

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

nemo
  • 55,207
  • 13
  • 135
  • 135
0

A few packages I found to do this:

Aidan Feldman
  • 5,205
  • 36
  • 46