[英]How pass numpy array from Python to Golang function
I'm trying create library using with Golang for working in Python.我正在尝试使用 Golang 创建库以在 Python 中工作。 But I have one problem: I can't pass numpy array to go function.但我有一个问题:我不能将 numpy 数组传递给 go 函数。 Please, help me solve problem.请帮我解决问题。
My code:我的代码:
main.go: main.go:
package main
import "C"
import "fmt"
type Matrix [][] int
//export Mass
func Mass(data Matrix) int {
var sum int=0
for i, x:= range data {
for j, _ := range x {
sum+=data[i][j]
}
}
return sum
}
func main() {
}
ex.py:例:
from ctypes import *
import numpy as np
mysum = cdll.LoadLibrary(r'D:\tests\go\sum.dll')
array_2d_int = np.ctypeslib.ndpointer(dtype=c_int32, ndim=2,
flags='CONTIGUOUS')
def mass(a):
mysum.Mass.argtypes = [array_2d_int]
mysum.Mass.restype = c_int32
return mysum.Mass(a)
Compiling cmd-line:编译 cmd 行:
go build -buildmode=c-shared -o sum.dll main.go
When I call function from dll-library:当我从 dll 库调用函数时:
from ex import mass
import numpy as np
a=np.array([[1,2],[3,4]])
print(mass(a))
I have errors:我有错误:
unexpected fault address 0x1b216000
fatal error: fault
[signal 0xc0000005 code=0x0 addr=0x1b216000 pc=0x6ac92c1f]
goroutine 17 [running, locked to thread]:
....
I dont know if this is what you are looking for.我不知道这是否是你要找的。
Execute the relevant commands in batch批量执行相关命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.