github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/example_test.go (about) 1 package luar_test 2 3 import ( 4 "fmt" 5 6 "github.com/dfcfw/lua" 7 "github.com/dfcfw/lua/luar" 8 ) 9 10 type User struct { 11 Name string 12 token string 13 } 14 15 func (u *User) SetToken(t string) { 16 u.token = t 17 } 18 19 func (u *User) Token() string { 20 return u.token 21 } 22 23 const script = ` 24 print("Hello from Lua, " .. u.Name .. "!") 25 u:SetToken("12345") 26 ` 27 28 func Example_basic() { 29 L := lua.NewState() 30 defer L.Close() 31 32 u := &User{ 33 Name: "Tim", 34 } 35 L.SetGlobal("u", luar.New(L, u)) 36 if err := L.DoString(script); err != nil { 37 panic(err) 38 } 39 40 fmt.Println("Lua set your token to:", u.Token()) 41 // Output: 42 // Hello from Lua, Tim! 43 // Lua set your token to: 12345 44 }