Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Develop plugins in Go?

Tags:

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 ?

like image 280
vvilp Avatar asked Sep 16 '12 02:09

vvilp


2 Answers

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.

like image 146
Denys Séguret Avatar answered Nov 02 '22 02:11

Denys Séguret


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

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

like image 41
weima Avatar answered Nov 02 '22 03:11

weima