python和golang调用_用C从python调用golang函数

我的myPyTest.c文件中有以下代码#include

#include "libtest.h"

static PyObject * callThatFunc(PyObject *self, PyObject *args) {

const char *a, *b, *c;

if (!PyArg_ParseTuple(args, "S", &a, &b, &c))

return NULL;

printf("%s", returnThis(a));

return PyString_FromString(b);

}

static PyMethodDef BarMethods[] = {

{"call_bar", callThatFunc, METH_VARARGS,

"Execute a shell command."},

{NULL, NULL, 0, NULL}

};

PyMODINIT_FUNC

initbar(void)

{

(void) Py_InitModule("bar", BarMethods);

}

int main() {

printf("Hello");

return 0;

}

检查this上面的代码

现在我有以下的歌郎代码在我的游戏_呼叫。开始文件。在

^{pr2}$

我用命令(MAC)创建了共享库

go build -ldflags -s -buildmode=c-shared -o libtest.so my_go_call.go

那我有设置.py为python构建c扩展的文件。代码设置.py具体如下:from distutils.core import setup, Extension

module1 = Extension('bar',

libraries = ['test'],

library_dirs = ['/path/to/lib'],

sources = ['myPyTest.c'])

setup (name = 'PackageName',version = '1.0',description = 'This is a demo package',ext_modules = [module1])

现在我使用以下命令构建和安装我的扩展python setup.py build

python setup.py install

每当我在python解释器中调用函数时,都会出现以下错误。在>>> import bar

>>> dir(bar)

['__doc__', '__file__', '__name__', '__package__', 'call_bar']

>>> bar.call_bar('a','b','c')

Traceback (most recent call last):

File "", line 1, in

TypeError: function takes exactly 1 argument (3 given)

你可能感兴趣的:(python和golang调用)