that library has a bunch of quite large and complicated structs that I need
to interface with.
While trying to do so I ran into a problem that I almost feel is a problem
with the cgo package itself. I've boiled it down to a pretty minimal
example to show what I mean, and it can be demonstrated with just a single
.h file and a main Go file.
A brief explanation and more details in the code below. When trying to use
a C struct that has other structs inside of it (anonymous structs) it is as
if cgo stops parsing the struct past that first nested struct. However it
still finds all the fields inside that first nested struct, but it puts
them all at the top level of the original struct. Anything past the first
nested struct doesn't get read at all, and when trying to print the object
they all just get passed in to what seems to be an array of raw bytes.
Hopefully the code below is clear enough.
Does anyone know what's going on here or how to circumvent the problem?
struct.h
---
typedef struct param_struct_t {
int a;
int b;
struct {
int c;
int d;
} anon;
int e;
struct {
int f;
int g;
} anon2;
} param_struct_t;
main.go
---
package main
/*
#include "struct.h"
*/
import "C"
import (
"fmt"
)
func main() {
var param C.param_struct_t
fmt.Println(param.a) // Works and should work
fmt.Println(param.b) // Works and should work
fmt.Println(param.c) // Works fine but shouldn't work
fmt.Println(param.d) // Works fine but shouldn't work
// fmt.Println(param.e) // Produces type error: ./main.go:17: param.e
undefined (type C.param_struct_t has no field or method e)
// fmt.Println(param.anon) // Produces type error: ./main.go:18:
param.anon undefined (type C.param_struct_t has no field or method anon)
// The following shows that the first parameters and the parameters from
the
// first anonymous struct gets read properly, but the subsequent things
are
// read as seemingly raw bytes.
fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0,
b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0}}
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.