I want to return a C struct value from a Go function. Assuming ProcessX()
and ProcessY()
are go methods which return integers (uint8 values):
package main
/*
struct Point {
char x;
char y;
};
*/
import "C"
//export CreatePoint
func CreatePoint(x uint8, y uint8) C.Point {
xVal := ProcessX(x);
yVal := ProcessY(y);
return C.Point {x: xVal, y: yVal}
}
func main() {}
But this results in a build error: ".\main.go:13:36: could not determine kind of name for C.Point
"
Edit:
Using go build -ldflags "-s -w" -buildmode=c-shared -o mylibc.dll .\main.go
to compile in Windows 10 through mingw
Edit-2:
I have removed the empty line between the "C" import and the preceeding preamble, but couldn't avoid the error. The code is now like:
/*
struct Point {
char x;
char y;
};
*/
import "C"
I find reading the documentation useful to solve problems like this.
To access a struct type directly, prefix it with struct_, as in C.struct_stat.
package main
/*
struct Point {
char x;
char y;
};
*/
import "C"
//export CreatePoint
func CreatePoint(x uint8, y uint8) C.struct_Point {
xVal := ProcessX(x);
yVal := ProcessY(y);
return C.struct_Point {x: C.char(xVal), y: C.char(yVal)}
}
func ProcessX(x uint8) uint8 { return x | 'x'}
func ProcessY(y uint8) uint8 { return y | 'y'}
func main() {}