-
Notifications
You must be signed in to change notification settings - Fork 1
Generated Files
header2go produces two files in the output directory. main.go
contains all the CGo boilerplate, including the exported
functions and any necessary C types we have to include. The functions in this file handle the mapping between C memory
and Go memory in a Cgo compliant fashion. Each of the exported functions calls a corresponding Go function in defs.go
.
The second file, defs.go
, is the skeleton Go implementation of the API. The idea is to completely hide the horrors
of CGo in main.go
, leaving defs.go
as a pure Go implementation. A Go struct is created for each C type used in
the exported functions, ensuring that all the function parameters in defs.go
are Go types.
To see examples of generated output, look in the translate/testdata
directory. The subdirectories contain examples
of an input header file and the corresponding main.go
and defs.go
. These files are used for unit testing.
Here is a quick example to save you jumping straight into the source. For this simple header file:
typedef struct {
int val3;
} StructB;
typedef struct _StructA {
int val1;
StructB val2;
} StructA;
void functionA(StructA a1, long a2);
we get two files:
// main.go
package main
/*
typedef struct {
int val3;
} StructB;
typedef struct _StructA {
int val1;
StructB val2;
} StructA;
*/
import "C"
func convertToStructB(s C.StructB) StructB {
return StructB{
Val3: int16(s.val3),
}
}
func convertToStructA(s C.StructA) StructA {
return StructA{
Val1: int16(s.val1),
Val2: convertToStructB(s.val2),
}
}
//export functionA
func functionA(a1 C.StructA, a2 C.long) {
goFunctionA(convertToStructA(a1), int32(a2))
}
func main() {}
// defs.go
package main
type StructB struct {
Val3 int16
}
type StructA struct {
Val1 int16
Val2 StructB
}
func goFunctionA(a1 StructA, a2 int32) {
// TODO implement goFunctionA
}