GOLANG中使用LUA进行扩展
嵌入lua后方便做功能扩展
package mainimport ("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))`) // 8L.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))