Skip to content

goinfi lua, another lua binding for Go

License

BSD-3-Clause, Unknown licenses found

Licenses found

BSD-3-Clause
LICENSE
Unknown
LICENSE-Lua
Notifications You must be signed in to change notification settings

hongy3025/goinfi.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

goinfi lua, another lua binding for Go

Resources

License

BSD-3-Clause, Unknown licenses found

Licenses found

BSD-3-Clause
LICENSE
Unknown
LICENSE-Lua

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published