github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/README.md (about)

     1  # gopher-luar [![GoDoc](https://godoc.org/layeh.com/gopher-luar?status.svg)](https://godoc.org/layeh.com/gopher-luar)
     2  
     3  gopher-luar simplifies data passing to and from [gopher-lua](https://github.com/yuin/gopher-lua).
     4  
     5  Example usage:
     6  
     7  ```go
     8  package luar_test
     9  
    10  import (
    11      "fmt"
    12  
    13      "github.com/dfcfw/lua"
    14      "github.com/dfcfw/lua/luar"
    15  )
    16  
    17  type User struct {
    18      Name  string
    19      token string
    20  }
    21  
    22  func (u *User) SetToken(t string) {
    23      u.token = t
    24  }
    25  
    26  func (u *User) Token() string {
    27      return u.token
    28  }
    29  
    30  const script = `
    31  print("Hello from Lua, " .. u.Name .. "!")
    32  u:SetToken("12345")
    33  `
    34  
    35  func Example_basic() {
    36      L := lua.NewState()
    37      defer L.Close()
    38  
    39      u := &User{
    40          Name: "Tim",
    41      }
    42      L.SetGlobal("u", luar.New(L, u))
    43      if err := L.DoString(script); err != nil {
    44          panic(err)
    45      }
    46  
    47      fmt.Println("Lua set your token to:", u.Token())
    48      // Output:
    49      // Hello from Lua, Tim!
    50      // Lua set your token to: 12345
    51  }
    52  ```
    53  
    54  ## License
    55  
    56  MPL 2.0
    57  
    58  ## Author
    59  
    60  Tim Cooper (<tim.cooper@layeh.com>)