I'm trying to build a go shared library with a function that returns a slice.
How can I use the slice from C code ?
package main
import "C"
type T struct {
A C.int
B *C.char
}
//export Test
func Test() []T {
arr := make([]T, 0)
arr = append(arr, T{C.int(1), C.CString("a")})
arr = append(arr, T{C.int(2), C.CString("abc")})
return arr
}
func main() {}
go build -o lib.so -buildmode=c-shared main.go
I now have a lib.so
and a lib.h
What would be the C code to print the values of the array ?
#include <stdio.h>
#include "lib.h"
typedef struct {
int A;
char* B;
} T;
int main() {
GoSlice a = Test();
for (int i = 0; i < 2; i++){
printf("%s
", ((T *)a.data)[i].B);
}
}
gcc -o main main.c ./lib.so