Alba Mendez
Alba Mendez

Reputation: 4635

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

Upvotes: 7

Views: 2484

Answers (1)

Miki Tebeka
Miki Tebeka

Reputation: 13910

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

Upvotes: 3

Related Questions