go-user-plugins is an open-source lib that act as user plugin generation from source , load and invoke a plugin method.
go get github.com/chen-keinan/go-user-plugins
// init plugin folder
userPlugin := NewPluginLoader("./soureFolder", "./objFolder")
sourcePluginName :="test.go"
// compile plugin from source
compiledPlugin, err := userPlugin.Compile(sourcePluginName)
if err != nil {
fmt.Print(err.Error())
}
// print compiled plugin name
fmt.Println(fmt.Sprintf("compiled plugin name %s", compiledPlugin))
// init plugin folder
userPlugin:= NewPluginLoader("./soureFolder", "./objFolder")
compiledPluginName :="test.so"
pluginMethodName := "Test"
// load and invoke plugin method
results, err :=userPlugin.LoadAndInvoke(compiledPluginName, pluginMethodName)
if err != nil {
fmt.Print(err.Error())
}
res := results[0].(string)
fmt.Println(fmt.Sprintf(res)
package main
//Test this plugin
func Test(value string) string {
return value
}
go build -buildmode=plugin -o ./objFolder/test.so ./soureFolder/test.go
package main
import (
"fmt"
"github.com/chen-keinan/go-user-plugins/uplugin"
"os"
)
func main() {
//Test this plugin
userPlugin := uplugin.NewPluginLoader("./soureFolder", "./objFolder")
compiledPluginName := "test.so"
pluginFuncdName := "Test"
// load and invoke plugin method
results, err := userPlugin.LoadAndInvoke(compiledPluginName, pluginFuncdName, "string value")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
res := results[0].(string)
fmt.Println(fmt.Sprintf(res)
}
Note: Plugin and binary must compile with the same linux env
code contribution is welcome !! , contribution with passing tests and linter is more than welcome :)