我将我的 python 文件用 cython 化为 example.c 和 example.so 我可以将这些文件导出为 python 模块并访问 python 中的函数 测试.py:
import example
example.test()
这样我就可以轻松地在 python 中导入我的 cythonized python 文件。 我如何使用 cgo 在 golang 中类似地导入这些文件?
最佳答案
Cython 代码包括一堆代码包装,用于创建 python 模块并在 Python 对象和 C 类型之间进行转换。
您可能不需要这些。例如,在 cgo 中一个非常简单的 hello world 可能只是:
/* hello.c */
#include <stdio.h>
static void hello(void) {
printf("hello world!\n");
}
并且您可以使用特殊导入和一些包含指令注释的 C 代码从 GO 调用它:
package main
// #include <hello.c>
import "C"
func main() {
C.hello()
}
你当然可以尝试只包含上面的“cythonized”C 文件,但你还需要包含适当的链接器指令,以便 Go 可以将所有 Python 运行时的东西编译到你的 Go 二进制文件中。
一般来说,这可能不是您想要做的...
你可能想看看像 http://grump.io/ 这样的东西如果你真的想从 Go 调用 Python。
关于python - 如何使用cgo在golang中导入cython生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094548/