This is a creation in Article, where the information may have evolved or changed.
preface
Recently in the project you need to use LUA to scale up and discover that there is a LUA virtual machine written by Golang on GitHub, named Gopher-lua. After use, it's good to see, Let's share it with you.
data type
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. The data in the
Golang is converted to data in Lua and must be converted to a type that begins with L:
str: = "Hello" num: = 10 l.lstring (str) l.lnumber (float64 (num))
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 of the type, If you do not know it, you must make a type judgment:
value: = L.get ( Span class= "Hljs-number" style= "Color:rgb (0,,)" >1 ) switch value. Type () {case lua.ltstring: case Lua. lttable: ....}
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.
funcHello (L *lua. State)int{//Return parameters are pressed into the stackL.push (LUA). Lstring ("Hello"))//Return parameter is 1 return 1}//injected into LUAL.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:
//Get the functions defined in LUA first fn : = L. Getglobal ("Hello")ifERR: = L.callbyparam (lua. p{Fn: fn, nret: 1, Protect: True,}, lua. Lnumber (Ten)); Err! = Nil {panic (err)}//Get function return value hereRET: = 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:
typeRolestruct{Namestring}typePersonstruct{NamestringAgeintWorkPlacestringRole []*role}l: = Lua. NewState ()ifERR: = L.dostring (' person = {name = ' Michel ', age = ', '--weakly input work_place = ' San Jose ', role = {{name = ' Admi Nistrator "}, {name =" Operator "}}} '); Err! =Nil{Panic(ERR)}varPerson personifERR: = 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:
funcLoader (L *lua. Lstate)int{//Register the export function in the moduleMoD: = L.setfuncs (l.newtable (), exports) L.push (MoD)return 1}varExports =Map[string]lua. lgfunction{"MyFunc": MyFunc,}funcMyFunc (L *lua. Lstate)int{return 0}//You can load the MyModule module hereL.preloadmodule ("MyModule", MyModule. Loader)