/goinfi.lua

goinfi lua, another lua binding for Go

Primary LanguageCBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Another lua binding for Go

Build and install
~~~~~~~~~~~~~~~~~~

# cd goinfi/lua
# go install

Test
~~~~~
# cd goinfi/lua
# go test

About API
~~~~~~~~~~

API of goinfi/lua is designed to very easy embedding lua script engine to Golang.
You are not necessarily to known what lua native c API is.

With goinfi/lua, we can export go function to lua like this:

// <go>
import "goinfi/lua"

func Host() string {
	return "Golang"
}
func Add(a, b int) int {
	return a + b
}

var vm lua.VM

func Init() {
	vm = lua.NewVM()
	vm.Openlibs()
	vm.AddFunc("Host", Host)
	vm.AddFunc("golang.Add", Add)
}
// </go>

And we can call lua script like this:

// <go>
vm.EvalString("print(Host())")
vm.EvalString("print(golang.Add(2, 3))")
// </go>

We can export go struct and it's method like this:
	
// <go>
type Point struct {
	X int
	Y int
}
func NewPoint(x, y int) *Point {
	return &Point{x, y}
}
func (p *Point) SumXY() int {
	return p.X + p.Y
}
type Rect struct {
	P0  Point
	P1  Point
}
func NewRect() *Rect {
	return &Rect{}
}
func Init() {
	vm = lua.NewVM()
	vm.Openlibs()
	vm.AddFunc("NewPoint", NewPoint)
	vm.AddFunc("NewRect", NewRect)
	vm.AddStructList(struct {
		*Point
		*Rect
	}{})
}
// </go>

And we can call lua script like this:

// <go>
vm.EvalString("point = NewPoint(1,2); print(point.X, point.Y, point:SumXY())"
vm.EvalString("rect = NewRect(); print(rect.P0_X, point.P1_Y)"
// </go>

More demo code can be found in lua/lua_test.go and lua/exam/*.go. 
Have Fun.