Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Plugin system" for Go

I'm looking for an equivalent of Architect for the Go language.

With Architect, modules expose "plugins". Plugins can specify dependencies, and export an API to allow interaction with other plugins. To start an application instance, you specify a list of plugins. Dependencies are resolved, and plugins are loaded (instantiated) in order.

Since each application creates a single instance of each plugin, multiple applications can be started in the same process without colliding.

Edit: I don't need the other modules to be loaded dynamically.

like image 252
Alba Mendez Avatar asked Jul 19 '14 11:07

Alba Mendez


1 Answers

I don't of a package that does that, but have some thoughts on how to do that - hope it'll help.

  • Use a build tag for each plugin.
  • Have each plugin (file) specify in a special comment/variable its dependencies
  • Run a pre build step that generate order of initialization (toplogical sort, fail on cycles). The output is a go file which is the function called by the plugin system initialization.
  • Have Registry and Plugin interfaces, probably something like:

    type Registry {
        // Register registers a plugin under name
        Register(name string, plugin *Plugin) error
        // Get plugin by name
        Get(name string) (*Plugin, error)
    }
    
    // Global Registry
    var GlobalRegistry Registry
    
    type Plugin interface {
        // Init is called upon plugin initialization. Will be in dependency order
        Init(reg Registry) error
        // Execute plugin command
        Exec(name string, args... interface{}) (interface{}, error)
    }
    
  • Run go build -tags plugin1,plugin2
like image 163
lazy1 Avatar answered Oct 20 '22 20:10

lazy1