首先須要本身如今linux下配置好go和lua環境linux
clua.lualua
function add(x,y)
return x + y
end
glua.go
code
package main
/*
#cgo pkg-config: lua5.1
#cgo luaa LDFLAGS: -llua -lm -ldl
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
lua_State* L;
int luaadd(int x, int y)
{
int sum;
lua_getglobal(L,"add");
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_call(L, 2, 1);
sum = (int)lua_tonumber(L, -1);
lua_pop(L,1);
return sum;
}
int call()
{
int sum;
L = lua_open();
luaL_openlibs(L);
luaL_dofile(L, "clua.lua");
sum = luaadd(10, 2);
lua_close(L);
return sum;
}
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.call())
}
而後直接go run golua.go就能夠了get