


How do I use gopher-lua to define a Lua function that takes a predefined table as a parameter and that the Lua script can access within the function?
php editor Xiaoxin will introduce to you how to use gopher-lua to define a function with predefined table parameters in the Lua function, and access the table within the function. gopher-lua is a Lua interpreter implemented in Go language, which can embed and execute Lua scripts in Go programs. Through reasonable code design and use, we can easily achieve this goal. Next, we'll explain in detail how to do this.
Question content
I know how to define a Go function and make it global (Double example in the documentation). But what if the parameter of this function should be a predefined table?
function calling_this_function_would_be_required(predefined_table) print(predefined_table["something"]) end
IMAP server Dovecot does provide something like the above: https://doc.dovecot.org/configuration_manual/authentication/lua_based_authentication/#examples
I would also like to provide predefined functions with tables (or even user data). But I don't really know how to achieve this.
Making a table global is easy (L.SetGlobal(...)), but how do you add it to the expected function?
Add some functions in 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)) }
It would be great if anyone could give me some inspiration :-) Thanks in advance
Solution
Based on @koyaanisqatsi's answer, I figured out how to make this work in Go.
Go code example:
<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 file:
<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>
result:
Mustermann Max Sackgasse 19 36304 Alsfeld The result of the Lua function is: 0
The above is the detailed content of How do I use gopher-lua to define a Lua function that takes a predefined table as a parameter and that the Lua script can access within the function?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Using Golang to implement Linux...

Regarding the problem of custom structure tags in Goland When using Goland for Go language development, you often encounter some configuration problems. One of them is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Performance optimization strategy for Go language massive URL access This article proposes a performance optimization solution for the problem of using Go language to process massive URL access. Existing programs from CSV...
