How pass numpy array from Python to Golang function

1

I'm trying create library using with Golang for working in Python. But I have one problem: I can't pass numpy array to go function. Please, help me solve problem.

My code:

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:

go build -buildmode=c-shared -o sum.dll main.go

When I call function from dll-library:

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]:
....
python
numpy
go
ctypes
asked on Stack Overflow Oct 12, 2018 by Michael

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0