-
When I use a Union in C, the code generated by bpf2go is not complete. Should I complete it manually? Is there a more elegant way to use it in Go? The C code is as follows: typedef struct
{
__u32 a1;
__u8 a2;
} A;
typedef struct
{
__u32 b1;
__u32 b2;
__u8 b3;
} B;
typedef struct
{
__u32 type;
union value
{
A a;
B b;
} value;
} Value; After compiling, the corresponding part in Go is as follows : type bpfValue struct {
Type uint32
Value struct {
A struct {
A1 uint32
A2 uint8
_ [3]byte
}
_ [4]byte
}
} There is only struct A. Completing it manually can easily lead to mistakes. Is there a more elegant way to use it in Go? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Since Go doesn't have unions, how would you 'complete' this type? The rationale behind the current behaviour is: on the first position in the union ('a' in this case), put the type you want to use to interact with the union from the Go side. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response. I know that Go doesn't have unions.What I meant is that not all the C structures have been fully implemented in Go. It is tedious and error-prone to manually implement these structures in Go. Today, I discovered that I can use |
Beta Was this translation helpful? Give feedback.
Have you tried passing
-type A -type B
tobpf2go
? That should make it generate both types.