This is a creation in Article, where the information may have evolved or changed.
Objective
Recently, in the project, it was necessary to use LUA to scale up and discover that there was a LUA virtual machine written in Golang on GitHub, named Gopher-lua. After using it, we found that it was good to share with you.
Data type
The data type in LUA corresponds to the data type in Golang the author has already explained in the document, it is worth noting that the type starts with L, and the name of the type begins with Lt.
Conversion of data in Golang to data in Lua must be converted to a type that begins with L:
str := "hello"num := 10L.LString(str)L.LNumber(float64(num))
The data in Lua is converted to data in Golang, and the project provides functions such as toint,checkstring to be converted, but this must be known in advance and must be typed if not known:
value := L.Get(1)switch value.Type() {case lua.LTString:case lua.LTTable:....}
The Gopher-luar can also be used to facilitate type conversion.
Golang and Lua invoke functions with each other
The function in Golang must be converted to func (L *lua. State) int This form can be injected into LUA, and the int that returns the parameter represents the number of returned parameters.
func hello(L *lua.State) int { //将返回参数压入栈中 L.Push(lua.LString("hello")) //返回参数为1个 return 1}//注入lua中L.SetGlobal("hello", L.NewFunction(hello))
In Golang, the LUA function is called, and the Lua script needs to define the function before calling Callbyparam to invoke it:
//先获取lua中定义的函数fn := L.GetGlobal("hello")if err := L.CallByParam(lua.P{ Fn: fn, NRet: 1, Protect: true, }, lua.LNumber(10)); err != nil { panic(err)}//这里获取函数返回值ret := L.Get(-1)
Table
The table in Lua is a very powerful thing, and the project has a lot of support for table. For example, get a field and add a field. It is recommended to use Gluamapper, which can convert tabl to struct in Golang or map[string] interface{} type, which uses the example provided by the author:
type Role struct { Name string}type Person struct { Name string Age int WorkPlace string Role []*Role}L := lua.NewState()if err := L.DoString(`person = { name = "Michel", age = "31", -- weakly input work_place = "San Jose", role = { { name = "Administrator" }, { name = "Operator" } }}`); err != nil { panic(err)}var person Personif err := gluamapper.Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil { panic(err)}fmt.Printf("%s %d", person.Name, person.Age)
Loading and using of modules
The LUA basic module is available in the project, and the modules can be loaded by calling Openlibs, including Io,math,os,debug. If you want to load yourself, you can use the Skipopenlibs parameter to skip.
If you want to develop your own library, the documentation also explains:
func Loader(L *lua.LState) int { //注册模块中的导出函数 mod := L.SetFuncs(L.NewTable(), exports) L.Push(mod) return 1}var exports = map[string]lua.LGFunction{ "myfunc": myfunc,}func myfunc(L *lua.LState) int { return 0}//这里就可以加载mymodule模块L.PreloadModule("mymodule", mymodule.Loader)
Conclusion
Of course, here are just a few basic uses, the project also has some unsupported areas, such as: package.loadlib. More places await the reader to explore by themselves, and will provide articles on source code analysis later.