0

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"

1 Answer 1

1

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.

cgo command

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() {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.