r/golang•
funk443

How should I represent a C struct in Cgo?

help

So I have a struct defined in C, which contains some other C structs, pointers, etc.

So do I create a 1-to-1 mapping go struct, and do conversion every time I need to call C functions.

Or do I simply do `type T C.T`, and then provide public getters/setters, so I can access the struct from another package, while being able to just pass the struct to the C function without additional hassles.

which way is more ideal in Go? Thanks in advance.

Posted Jun 17, 2026, 1:09 PM UTC

199
Loading comments…