我知道如何定义Go函数并将其设置为全局函数(文档中的Double示例)。但是,如果这个函数的参数应该是一个预定义的表呢?
function calling_this_function_would_be_required(predefined_table)
print(predefined_table["something"])
end
字符串
IMAP服务器Dovecot确实提供了类似上面的内容:https://doc.dovecot.org/configuration_manual/authentication/lua_based_authentication/#examples
我也想提供预定义的功能表(甚至用户数据)。但我真的不知道如何实现这一点。
将表设置为全局很容易(L.SetGlobal(...)),但如何将其添加到预期的函数中?
在Go中添加函数
func CallMe(L *lua.LState) {
// How do I add a table as argument??
}
func Foo() {
L := NewState()
defer L.Close()
t := L.NewTable()
t.RawSetString("example", lua.LString("some_value"))
// I do not want a global table. I would like an expected Lua function that has _this_ table as argument
L.SetGlobal("predefined_table", t)
// Not even sure with his...
L.SetGlobal("calling_this_function_is_required", L.NewFunction©llMe))
}
型
如果有人能给我一点启发,那就太好了:-)提前感谢
2条答案
按热度按时间uidvcgyl1#
基于@koyaanisqatsi的回答,我发现了如何在Go中工作。
Go代码示例:
字符串
sample.lua文件:
型
测试结果:
型
c86crjj02#
如果省略参数,则可以返回预定义的表。
例如
字符串
返回表的构造函数将所有表函数作为方法附加。
使用示例(Lua 5.1 Standalone)
型