| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
|
| 7 |
"github.com/brentp/goluaez" |
| 8 |
) |
| 9 |
|
| 10 |
func check(e error) { |
| 11 |
if e != nil { |
| 12 |
log.Fatal(e) |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
func main() { |
| 17 |
L, err := goluaez.NewState(` |
| 18 |
adder = function(a, b) |
| 19 |
return a + b |
| 20 |
end`) |
| 21 |
check(err) |
| 22 |
defer L.Close() |
| 23 |
|
| 24 |
// Run uses a mutex so can be run in a goroutine. |
| 25 |
result, err := L.Run("adder(x, y)", map[string]interface{}{"x": 12, "y": "23"}) |
| 26 |
check(err) |
| 27 |
fmt.Println(result.(float64)) |
| 28 |
// 35 |
| 29 |
|
| 30 |
result, err = L.Run("'hello' .. ' world'") |
| 31 |
check(err) |
| 32 |
fmt.Println(result.(string)) |
| 33 |
// hello world |
| 34 |
|
| 35 |
L.DoString("a = {}; a['a'] = 22; a['b'] = 33;") |
| 36 |
result, err = L.Run("a") |
| 37 |
check(err) |
| 38 |
fmt.Println(result.(map[string]interface{})) |
| 39 |
// map[b:33 a:22] |
| 40 |
|
| 41 |
} |