zvvq技术分享网

如何把 Golang 与 python 集成?

作者:zvvq博客网
导读如何将 golang 与 python 集成?安装 golang 和 python。配置 gopath 环境变量。安装 cgo golang 包。创建 golang 项目。为 python 组件编写 golang wrapper 函数。在 python 项目中创建模块。使用 cgo 构建

如何把 golang 与 python 集成?组装 golang 和 python。配备 gopath 环境变量。组装cgo golang 包。建立 golang 项目。为 python部件撰写 golang wrapper函数。在 python工程中建立模块。应用 cgo 搭建 golang 项目。在 golang 代码中启用 python函数。运作 golang 程序。

内容来自samhan

本文来自zvvq

Golang 架构与 Python 集成

内容来自samhan

有时,您可能需要在 Golang 应用程序中应用 Python 部件。本指南将向您展现如何把 Golang 与 Python 集成,并提供一个实战案例 。

内容来自samhan666

前提条件 内容来自zvvq,别采集哟

组装 Golang 和 Python 配备 GOPATH 环境变量组装名叫 cgo 的 Golang 包步骤

zvvq好,好zvvq

建立 Golang 项目1

zvvq.cn

mkdirgolang-python-integration&&cdgolang-python-integration

内容来自samhan

撰写 Golang wrapper为 Python 部件创建一个 Golang wrapper函数。这将允许您在 Golang 代码中启用 Python 函数。

内容来自samhan666

//mypython.go

zvvq

packagemain

内容来自samhan

/ 内容来自zvvq,别采集哟

include

zvvq

staticPyObjectcall_python_function(charfunc_name){ 内容来自samhan

PyObjectpName,pModule,pDict,pFunc,pValue; 内容来自zvvq

pName=PyUnicode_DecodeFSDefault(func_name); 内容来自samhan

pModule=PyImport_Import(pName);

zvvq

pDict=PyModule_GetDict(pModule);

zvvq好,好zvvq

pFunc=PyDict_GetItemString(pDict,"my_function");

内容来自zvvq,别采集哟

pValue=PyObject_CallObject(pFunc,NULL);

zvvq.cn

returnpValue;

zvvq好,好zvvq

}

copyright zvvq

/

copyright zvvq

import"C"

zvvq.cn

import(

内容来自zvvq

"fmt" 本文来自zvvq

"unsafe"

copyright zvvq

)

内容来自samhan666

funcmain(){ copyright zvvq

result:=C.call_python_function(C.CString("my_function"))

内容来自zvvq

fmt.Println(C.GoString(result)) zvvq.cn

}

内容来自zvvq

撰写 Python 模块在 Python 工程中建立 my_function 模块。

zvvq.cn

my_function.py 内容来自samhan666

defmy_function():

内容来自zvvq

return"HellofromPython!"

zvvq

搭建 Golang 项目应用 cgo 搭建 Golang 项目。它将允许 Golang 程序启用 C 编码,后者又把启用 Python 编码。

zvvq.cn

gobuild-buildmode=c-shared-omypython.somypython.go 内容来自samhan

启用 Python 函数在 Golang 代码中,您可以如今启用 Python 函数。

内容来自samhan

packagemain zvvq

import(

本文来自zvvq

"fmt" 内容来自zvvq

"log" zvvq.cn

"syscall" 本文来自zvvq

"unsafe" zvvq.cn

) 内容来自zvvq,别采集哟

funcmain(){

copyright zvvq

//Loadthesharedlibrary. 本文来自zvvq

lib,err:=syscall.LoadDLL("mypython.so") zvvq

iferr!=nil{

zvvq.cn

log.Fatal(err)

zvvq.cn

} 内容来自zvvq

deferlib.Release() 内容来自zvvq,别采集哟

//Getthefunctionpointer. zvvq

callPythonFunction,err:=lib.FindProc("call_python_function")

zvvq.cn

iferr!=nil{

内容来自zvvq,别采集哟

log.Fatal(err)

内容来自zvvq,别采集哟

}

zvvq

//Callthe Python function.

内容来自zvvq,别采集哟

result,_,err:=callPythonFunction.Call( 本文来自zvvq

uintptr(unsafe.Pointer(syscall.StringBytePtr("my_function"))),

zvvq.cn

)

zvvq.cn

iferr!=nil{ 本文来自zvvq

log.Fatal(err) 本文来自zvvq

} 内容来自samhan

//Printtheresult. 内容来自zvvq,别采集哟

fmt.Println(syscall.UTF16ToString(([1<<30]uint16)(unsafe.Pointer(result))[:])) 内容来自zvvq,别采集哟

} zvvq

运作

zvvq好,好zvvq

运作 Golang 程序。 内容来自samhan

gorunmain.go

内容来自zvvq,别采集哟

导出 内容来自samhan666

HellofromPython!

zvvq好,好zvvq

您现在已成功将 Golang 与 Python 集成,并调用了 Python 函数。 zvvq.cn

以上就是Golang架构如何跟Python集成?的详细内容,大量请关注其他类似文章!

内容来自samhan