如何使用 gopher-lua 定义一个 Lua 函数,该函数有一个预定义的表作为参数,Lua 脚本可以在该函数中访问该表?
php小编小新将为你介绍如何使用gopher-lua在Lua函数中定义一个带有预定义表参数的函数,并且在函数内部访问该表。gopher-lua是一个Go语言实现的Lua解释器,可以在Go程序中嵌入和执行Lua脚本。通过合理的代码设计和使用,我们可以轻松实现这一目标。接下来,我们将详细讲解如何做到这一点。
问题内容
我知道如何定义 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)) }
如果有人能给我一点启发,那就太好了:-)提前致谢
解决方法
根据 @koyaanisqatsi 的回答,我找到了如何在 Go 中工作。
Go代码示例:
<code>package main import ( "fmt" "github.com/yuin/gopher-lua" ) type Person struct { Name string GivenName string Street string PostalCode string City string } func main() { p := &Person{ Name: "Mustermann", GivenName: "Max", Street: "Sackgasse 19", PostalCode: "36304", City: "Alsfeld", } L := lua.NewState() defer L.Close() if err := L.DoFile("sample.lua"); err != nil { panic(err) } t := L.NewTable() t.RawSetString("name", lua.LString(p.Name)) t.RawSetString("given_name", lua.LString(p.GivenName)) t.RawSetString("street", lua.LString(p.Street)) t.RawSetString("postal_code", lua.LString(p.PostalCode)) t.RawSetString("city", lua.LString(p.City)) if err := L.CallByParam(lua.P{ Fn: L.GetGlobal("call_me"), NRet: 1, Protect: true, }, t); err != nil { panic(err) } ret := L.Get(-1) // returned value L.Pop(1) // remove received value fmt.Println("The result of the Lua function is:", ret) } </code>
sample.lua文件:
<code>function call_me(tbl) print(tbl.name) print(tbl.given_name) print(tbl.street) print(tbl.postal_code) print(tbl.city) return 0 end </code>
结果:
Mustermann Max Sackgasse 19 36304 Alsfeld The result of the Lua function is: 0
以上是如何使用 gopher-lua 定义一个 Lua 函数,该函数有一个预定义的表作为参数,Lua 脚本可以在该函数中访问该表?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

Go语言中哪些库是大公司开发或知名开源项目?在使用Go语言进行编程时,开发者常常会遇到一些常见的需求,�...

Go语言中结构体定义的两种方式:var与type关键字的差异Go语言在定义结构体时,经常会看到两种不同的写法:一�...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

Go指针语法及viper库使用中的寻址问题在使用Go语言进行编程时,理解指针的语法和使用方法至关重要,尤其是在...
