| 1 |
luaez |
| 2 |
===== |
| 3 |
|
| 4 |
[](https://godoc.org/github.com/brentp/goluaez) |
| 5 |
[](https://travis-ci.org/brentp/goluaez) |
| 6 |
[](https://coveralls.io/github/brentp/goluaez?branch=master) |
| 7 |
|
| 8 |
|
| 9 |
Easy embedding of lua in go. |
| 10 |
|
| 11 |
`goluaez` wraps [gopher-lua](https://github.com/yuin/gopher-lua) and [gopher-luar](https://github.com/layeh/gopher-luar). `gopher-luar` does a nice job of converting go values to lua. This package uses that and also converts lua values to go and converts go slices to lua tables. |
| 12 |
|
| 13 |
This makes it easy to have small 1 or 2 line user-defined functions in a go application that embeds lua. |
| 14 |
|
| 15 |
Example usage: |
| 16 |
|
| 17 |
```Go |
| 18 |
package main |
| 19 |
|
| 20 |
import ( |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
|
| 24 |
"github.com/brentp/goluaez" |
| 25 |
) |
| 26 |
|
| 27 |
func check(e error) { |
| 28 |
if e != nil { |
| 29 |
log.Fatal(e) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func main() { |
| 34 |
L, err := goluaez.NewState(` |
| 35 |
adder = function(a, b) |
| 36 |
return a + b |
| 37 |
end`) |
| 38 |
check(err) |
| 39 |
defer L.Close() |
| 40 |
|
| 41 |
// Run uses a mutex so can be run in a goroutine. |
| 42 |
result, err := L.Run("adder(x, y)", map[string]interface{}{"x": 12, "y": "23"}) |
| 43 |
check(err) |
| 44 |
fmt.Println(result.(float64)) |
| 45 |
// 35 |
| 46 |
|
| 47 |
result, err = L.Run("'hello' .. ' world'") |
| 48 |
check(err) |
| 49 |
fmt.Println(result.(string)) |
| 50 |
// hello world |
| 51 |
|
| 52 |
L.DoString("a = {}; a['a'] = 22; a['b'] = 33;") |
| 53 |
result, err = L.Run("a") |
| 54 |
check(err) |
| 55 |
fmt.Println(result.(map[string]interface{})) |
| 56 |
// map[b:33 a:22] |
| 57 |
|
| 58 |
} |
| 59 |
``` |
| 60 |
|
| 61 |
Prelude |
| 62 |
------- |
| 63 |
|
| 64 |
The embedded lua engine is populated with the lua functions in data/prelude.lua |
| 65 |
So far, this consists of: |
| 66 |
|
| 67 |
+ string:split(sep) (split a string by a delim) |
| 68 |
+ string:strip() (remove whitespace from a string) |