How to return a C struct value from a go function

Issue

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"

Solution

There must be no newline between the import directive and the actual C-Code:

package main

/*
  struct Point {
    char x;
    char y;
};
*/
import "C"

// export CreatePoint
func CreatePoint(x uint8, y uint8) C.Point {
    return C.Point {x: x, y: y}
}

func main() {}

Answered By – h0ch5tr4355

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.