ggzeng
10/18/2019 - 2:31 AM

call go function from lua

在lua中调用go中的函数

package main

import lua "github.com/yuin/gopher-lua"

func Double(L *lua.LState) int {
    lv := L.ToInt(1)             /* get argument */
    L.Push(lua.LNumber(lv * 2))  /* push result */
    return 1                     /* number of results */
}

func main() {
  L := lua.NewState() // 创建一个lua解释器实例
	defer L.Close()
  
	L.SetGlobal("double", L.NewFunction(Double)) /* Original lua_setglobal uses stack... */
	if err := L.DoFile("test.lua"); err != nil {
        panic(err)
    }
}
print(double(20))    -- > 40