go-user-plugins

command module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL