forked from yuin/gluamapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (38 loc) · 741 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"github.com/Azure/golua/lua"
"github.com/Azure/golua/std"
"github.com/jdolitsky/goluamapper"
)
type Person struct {
Name string
Age int
}
func main() {
state := lua.NewState()
defer state.Close()
std.Open(state)
// Evaluate the Lua script.
// We can also use state.ExecFile(<filepath>)
err := state.ExecText(`
local name = "Fred"
local age = 42
person = {
name = name,
age = age
}
`)
if err != nil {
panic(err)
}
// Extract the "person" global var and map to Person type
var person Person
state.GetGlobal("person")
err = goluamapper.Map(state.Pop(), &person)
if err != nil {
panic(err)
}
// Should print "Fred 42"
fmt.Printf("%s %d\n", person.Name, person.Age)
}