英文:
Strugglign to pass string and int parameters from Python to Go library
问题
我有以下签名的Go库:
//export getData
func getData(symbol string, day int, month string, year int) string {
return "getData2"
}
在Python中,我像下面这样做:
import ctypes
library = ctypes.cdll.LoadLibrary('./lib.so')
get_data = library.getData
# 让Python将其值转换为C表示形式。
get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
get_data.restype = ctypes.c_wchar_p
j = get_data(b"XYZ", 3, "JAN", 23)
print(j.decode('utf-8'))
它会报错:
ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type
我正在使用Python 3.9
更新
我对Go函数签名进行了更改,如下所示:
func getData(symbol, day, month, year *C.char) *C.char {
var instrumentName, combine string
x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
log.Println(x)
....
在Python中,我进行了如下更改:
get_data = library.getData
# 让Python将其值转换为C表示形式。
# get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
get_data.restype = ctypes.c_wchar_p
j = get_data("BTC", "3", "JAN", "23")
# j = get_data(b"BTC", 3, "JAN", 23)
print(j.decode('utf-8'))
没有参数问题,但我遇到的问题是它在Go代码中获取每个参数的第一个字符,即:
x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
log.Println(x)
所以它打印的不是BTC-3JAN23
,而是B-3J2
。
英文:
I have the go library with the following signature:
//export getData
func getData(symbol string, day int, month string, year int) string {
return "getData2"
}
In Python I did like the below:
import ctypes
library = ctypes.cdll.LoadLibrary('./lib.so')
get_data = library.getData
# Make python convert its values to C representation.
get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
get_data.restype = ctypes.c_wchar
# j= get_data("BTC".encode('utf-8'), "3", "JAN".encode('utf-8'), "23".encode('utf-8'))
j= get_data(b"XYZ", 3, "JAN", 23)
print(j)
and it gives the error
ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type
I am using Python 3.9
Updates
I made changes in Go Function Signature like this:
func getData(symbol, day, month, year *C.char) *C.char {
var instrumentName, combine string
x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
log.Println(x)
....
And in Python like this:
get_data = library.getData
# Make python convert its values to C representation.
# get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
get_data.restype = ctypes.c_wchar_p
j= get_data("BTC", "3", "JAN", "23")
# j= get_data(b"BTC", 3, "JAN", 23)
print(j.decode('utf-8'))
No parameter issue but the issue I am getting that it is fetching first param of each param in Go code, that is:
x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
log.Println(x)
So instead of printing BTC-3JAN23
, it prints B-3J2
答案1
得分: 1
在第4行,你写的是get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
,但是由于第3个参数是一个字符串,所以应该改为get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_char_p,ctypes.c_int]
。
英文:
On line 4 you did get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
but since the 3rd argument is a string then it should be get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_char_p,ctypes.c_int]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论