This works just fine, however I am facing problems when my go function takes as input a slice and from C# an array.
As an example
//Sum :
//export Sum
func Sum(a []int) int {
return a[0] + a[1]
}
[DllImport("main", EntryPoint = "Sum")]
extern static int Sum(int[] array);
var numlis = new List<int>();
numlis.Add(1);
numlis.Add(5);
int[] array = numlis.ToArray();
int c = Sum(array);
Console.WriteLine("Call Go func Sum, result is " + c);
I get
The program ‘[15300] InitialConnect.exe’ has exited with code 2 (0x2).
Did you generate a .h file? If so, check its signature for your Sum function? It’s unlikely C# will generate the proper struct to represent a Go slice automatically. I once made a shared lib to be called by Python, and I had to wrap my functions that worked on slices with functions that accepted pointers and lengths as one would do in C. The wrapper functions created slice headers from the pointer and length before calling the original function that accepted a slice, like this
header := (*reflect.SliceHeader)(unsafe.Pointer(&out))
header.Data = uintptr(unsafe.Pointer(ptr))
header.Len = n
header.Cap = n
An alternate approach could be instead of creating the slice in Go code, you could probably create the GoSlice struct in your C# code. Either way, it’s not going to be transparent/automatic.