Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rolancia/go-lua-builder
go-lua supports scripting Lua in Golang
https://github.com/rolancia/go-lua-builder
go go-lua go-lua-builder go-lua-script go-lua-scripting lua lua-builder lua-script
Last synced: 10 days ago
JSON representation
go-lua supports scripting Lua in Golang
- Host: GitHub
- URL: https://github.com/rolancia/go-lua-builder
- Owner: rolancia
- License: mit
- Created: 2021-10-26T14:22:48.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-26T12:57:36.000Z (about 3 years ago)
- Last Synced: 2024-06-20T16:47:49.681Z (7 months ago)
- Topics: go, go-lua, go-lua-builder, go-lua-script, go-lua-scripting, lua, lua-builder, lua-script
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-lua-builder
go-lua-builder supports scripting Lua in Golang.
eg. Redis Lua Scripts can be managed in Golang as Code
---
### Variable
```lua
-- Lua
local a = nil
local b = "hello world"
local c = 2021
local d = truea = b
b = "hi world"local t = {name="rolancia"}
t["name"] = "tae"
t[1] = 1994local arr = {1,2,"hi"}
arr[4] = 4
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
a := l.Local(lua.Nil())
b := l.Local(lua.Str("hello world"))
c := l.Local(lua.Num(2021))
d := l.Local(lua.Bool(true))
l.Assign(a, b)
l.Assign(b, lua.Str("hi world"))
t := l.Local(lua.Table(map[string]lua.Object{
"name": lua.Str("rolancia"),
}))
l.Assign(lua.At(t, lua.Str("name")), lua.Str("tae"))
l.Assign(lua.At(t, lua.Num(1)), lua.Num(1994))arr := l.Local(lua.Array(lua.Num(1), lua.Num(2), lua.Str("hi")))
l.Assign(lua.At(arr, lua.Num(4)), lua.Num(4))
})
```### Operator
```lua
-- Lua
local a = false
local b = not a
b = alocal c = a + 1
print(c, 5 * 10)
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
a := l.Local(lua.Bool(false))
b := l.Local(lua.Not(a))
l.Assign(b, a)
// l.Do(lua.Op3(b, lua.Op("="), a))
c := l.Local(lua.Sum(a, lua.Num(1)))
// lua.Op3(a, lua.Op("+"), lua.Num(1))
lualib.Print(l, c, lua.Mul(lua.Num(5), lua.Num(10)))
// lua.Op3(lua.Num(5), lua.Op("*"), lua.Num(10))
})```
### Function Call
```lua
-- Lua
local a = "hello"
print(a, "world")
local n = tonumber("12345")
print(n)
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
a := l.Local(lua.Str("hello"))
lualib.Print(l, a, lua.Str("world"))
n := l.Local(lualib.ToNumber(l, lua.Str("12345")))
lualib.Print(l, n)
})
```### IF Statement
```lua
-- Lua
local a = 1
local b = 2
local c = 0
if a < b then
c = a
else
c = b
end
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
a := l.Local(lua.Num(1))
b := l.Local(lua.Num(2))
c := l.Local(lua.Num(0))
//l.If(lua.Cond(a, lua.Op("<"), b)).Then(func() {
l.If(lua.Cond(a, lua.Lt(), b)).Then(func() {
l.Assign(c, a)
}).Else(func() {
l.Assign(c, b)
}).End()
})
```### For
```lua
-- Lua
for i = 1,10,1
do
print(i)
endfor i = 10,1,-1
do
if i >= 5 then
print(i)
end
end
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
l.For(1, 10, 1).Do(func(i lua.Variable) {
lualib.Print(l, i)
})
l.For(10, 1, -1).Do(func(i lua.Variable) {
l.If(lua.Cond(i, lua.Gte(), lua.Num(5))).Then(func() {
lualib.Print(l, i)
}).End()
})
})
```### Return
```lua
-- Lua
return "bye world"
return "hello","world",100
return {1,2,3}
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
l.Return(lua.Str("bye world"))
l.Return(lua.Str("hello"), lua.Str("world"), lua.Num(100))
l.Return(lua.Array(lua.Num(1), lua.Num(2), lua.Num(3)))
})
```### Appending Any Lua Code
```lua
-- Lua
local a = 5
print(a)
``````go
// Go
_ = lua.NewLua(func (l *lua.DefaultBuilder) {
l.Append([]byte("local a = 5"))
l.AppendLine()
l.Append([]byte("print(a)"))
})
```---
## Features- Basic Syntax
- Operator
- Loop
- ~~Iterator~~ - planned
- Function Call
- ~~Function Definition in Lua~~ - planned
- ~~String Method~~ - planned
- Array
- Table
- ~~Module~~ - planned
- ~~Redis Lua Library~~ - planned
- ~~Math Library~~ - planned
- ~~Lua embeded functions~~ planned