Skip to content

chen-keinan/go-user-plugins

Repository files navigation

Go Report Card License test coverage badge Gitter
golang plugin logo

go-user-plugins

go-user-plugins is an open-source lib that act as user plugin generation from source , load and invoke a plugin method.

Installation

go get github.com/chen-keinan/go-user-plugins

Usage

Compile plugin from source

// 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))

Load and invoke a compiled plugin

// 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)

Full Example

test.go (source plugin)

package main

//Test this plugin
func Test(value string) string {
	return value
}

Compile plugin

 go build -buildmode=plugin -o ./objFolder/test.so ./soureFolder/test.go

Load and invoke plugin

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

Contribution

code contribution is welcome !! , contribution with passing tests and linter is more than welcome :)

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published