嵌入lua后方便做功能扩展
package main
import (
"fmt"
"github.com/yuin/gopher-lua"
)
func Double(L *lua.LState) int {
lv := L.ToInt(1) /* get argument */
L.Push(lua.LNumber(lv * 2)) /* push result */
return 1 /* number of results */
}
func SayHello(L *lua.LState) int {
name := L.ToString(1)
fmt.Println("hello ", name)
return 0
}
func main() {
L := lua.NewState()
defer L.Close()
L.SetGlobal("double", L.NewFunction(Double))
L.SetGlobal("sayHello", L.NewFunction(SayHello))
L.DoString(`print(double(4))`) // 8
L.DoString(`sayHello("wangjunsheng")`) // hello wangjunsheng
// L.DoFile("hello.lua") // from lua file
}
以下 emacs 中 lua-mode 写给自己看的
合适的位置 git clone https://github.com/immerrr/lua-mode.git
(add-to-list 'load-path "~/emacs/lua-mode")
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode))
(add-to-list 'interpreter-mode-alist '("lua" . lua-mode))
Golang嵌入Lua扩展
本文介绍如何在Golang中嵌入Lua脚本语言来实现功能扩展。通过具体示例展示了定义Lua函数并在Go中调用的方法,包括翻倍数值和打印问候消息的功能。
6838

被折叠的 条评论
为什么被折叠?



