The staticplug
Go module implements compile-time plugins. Each plugin is
a type that gets compiled into the executable. Once the executable is built its
plugins and their functionality is fixed.
Modules wanting to make use of plugins need to create a registry:
var Registry = staticplug.NewRegistry()
Plugins register with the registry as part of the process initialization:
func init() {
pkg.Registry.MustRegister(&myPlugin{})
}
The extendable module discovers plugins using implemented interfaces:
func Do() {
plugins, err := Registry.PluginsImplementing((*Calculator)(nil))
…
for _, info := range plugins {
p, err := info.New()
…
log.Printf("sum = %d", p.(Calculator).Add(1, 2))
}
}