plugin

package module
v0.0.0-...-24042ec Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2017 License: MIT Imports: 9 Imported by: 0

README

Go-Plugin

Example

main.go
package main

import (
	"fmt"
	"github.com/alaingilbert/go-plugin"
)

func LuaCanCallMe() string {
	return "Hello World!"
}

func main() {
	plugin.Init()
	defer plugin.Close()

	// Make the function available inside the plugins
	plugin.Set("LuaCanCallMe", LuaCanCallMe)

	p, err := plugin.Load("./myPlugin.lua")

	ret1, err := plugin.Call("myPlugin.HelloWorld")
	
	ret2, err := p.Call("OnSomeEvent")

	var squared int
	err = p.CallUnmarshal(&squared, "Square", 2)

	// Call each loaded plugins "HelloWorld" function
	plugin.Each(func(p plugin.Plugin) {
		if ret, err := p.Call("HelloWorld"); err == nil {
			fmt.Println(ret)
		}
	})

	fmt.Println(ret1, ret2, err)
}
myPlugin.lua
function HelloWorld()
    return LuaCanCallMe()
end

function OnSomeEvent()
    return "Some event process"
end

function Square(x)
    return x * x
end

Documentation

https://godoc.org/github.com/alaingilbert/go-plugin

Thanking

Heavily inpsired by micro plugins system.

https://github.com/zyedidia/micro

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	L *lua.LState
)

L is the lua state This is the VM that runs the plugins

Functions

func Call

func Call(fn string, args ...interface{}) (lua.LValue, error)

Call a function in the lua VM

func CallUnmarshal

func CallUnmarshal(v interface{}, fn string, args ...interface{}) error

func Close

func Close()

Close lua VM

func Each

func Each(clb func(p Plugin))

Each Call clb with each plugins sorted by name

func Init

func Init()

Init the lua VM

func IsLoaded

func IsLoaded(path string) bool

IsLoaded returns whether the file is loaded or not in the lua VM

func Set

func Set(name string, val interface{})

Set a global variable in lua VM

func Unload

func Unload(path string) error

Unload remove plugin functions from lua VM

Types

type Plugin

type Plugin struct {
	Path string
	Name string
}

Plugin ...

func Load

func Load(path string) (Plugin, error)

Load plugin functions in lua VM

func (*Plugin) Call

func (p *Plugin) Call(fn string, args ...interface{}) (lua.LValue, error)

Call a function for the specific plugin

func (*Plugin) CallUnmarshal

func (p *Plugin) CallUnmarshal(v interface{}, fn string, args ...interface{}) error

Call function, put result in v

func (*Plugin) Reload

func (p *Plugin) Reload() error

Reload a specific plugin

func (*Plugin) Unload

func (p *Plugin) Unload() error

Unload a specific plugin

Jump to

Keyboard shortcuts

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