duanmao1975 2019-05-18 12:59
浏览 91

将字符串作为参数从Python传递到共享库中的Go函数

I'm trying to call the below MakeQuery(query string) function by passing the string from Python using ctypes module.

It works when I call it from Python as a function shared object with hardcoded arguments from Works() in Go:

//export Works
func Works() {
    newMod := MakeQuery(`SELECT TOP 3 NodeID FROM NCM.ConfigArchive`)
    fmt.Println(newMod)
}

// Query db connection and return results as map
//export MakeQuery
func MakeQuery(query string) []map[string]interface {} {...}

According to some web tutorials, I need to convert it as such:

from ctypes import *

lib = cdll.LoadLibrary("./test.so")
query = "SELECT TOP 3 NodeID FROM NCM.ConfigArchive"
b_query = query.encode('utf-8')
lib.MakeQuery.argtypes = [c_char_p]
print lib.MakeQuery(b_query)

But it doesn't work due to some string type conversion error:

&{400 Bad Request 400 HTTP/1.1 1 1 map[Content-Length:[1602] Content-Type:
[application/json] Date:[Sat, 18 May 2019 12:48:54 GMT] Server:[Microsoft-
HTTPAPI/2.0]] 0xc00009a1c0 1602 [] false false map[] 0xc000104000 0xc0001a8000}

Would be a great help if someone can point me to the direction to convert Python's string type into Go in some way.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧