Last active
October 21, 2019 11:34
-
-
Save ggzeng/1c904457fe95ab11b4ae8efbca1d85cc to your computer and use it in GitHub Desktop.
在lua中调用go中的函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import lua "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 main() { | |
L := lua.NewState() // 创建一个lua解释器实例 | |
defer L.Close() | |
L.SetGlobal("double", L.NewFunction(Double)) /* Original lua_setglobal uses stack... */ | |
if err := L.DoFile("test.lua"); err != nil { | |
panic(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(double(20)) -- > 40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment