How to get C Structs from Go Structs( generated from chidley library)

Hi All,

I am trying to get the C lang Structures from the Go Structs which got generated (by $ chidley -G xml/test1.xml).

Could any one help me in getting C structs from Go Structs? Is there any library, Which takes the Go Structs as input and generate C structs ?

Thanks
Ravinder

In general, Go structs cannot be translated to C structs.

What are you trying to accomplish by having those C structs?

Hi nathankerr,

Let me tell you what I am doing,

as the first thing I have got xml, which has to be serialized and de- serialized. Here I got to know that GO lang will do this serialization and de-serialization i.e. it will create structures from the xml, and we have got that from chidley. As my legacy code is in C and which is dependent on this Structures, this is the reason why I want to convert Go Structs to C Structs.

Hope I have answered your question.

Thanks
Ravinder

Are you intending to only use Go for the serialization and de-serialization?

What sort of interaction are you expecting to do between Go and C?

At present our goal is to use Go lang for Serialization and De- Serialization.

the whole objective here is to parse the xml and generate the C structure. To achieve this, we have found like GO will generate the Structures from the xml. We could able to get the structures generated from Chidley. Now this Structures has to passed to C.

Interaction here is structure between Go and C.

The structures generated by Chidley will use go types. Go types are not the same a C types. Even simpler types like integers have differences. While both have an int type, they are different sizes. On my x86_64 a C int is 4 bytes while a go int is 8. Strings are also different; as are arrays. There is also the small matter that Go is garbage collected and C is not. GC means you have to be really careful about how memory is managed in both systems.

In short, the interoperation between Go and C is not trivial. You won’t be able to just use the structs generated by Chidley.

Since you referred to your C codebase as “legacy”, I assume that you are trying to incrementally move it into Go. I think that putting a data structure that is complicated enough that it needs to be generated on the Go side and all the processing on that data structure on the C side is a terrible idea. I suggest you find a different interaction point to start with. Doing so will give you experience in dealing with the interaction between Go and C so that when you have to deal with this difficult part you will have a better idea on how to do so.

https://golang.org/cmd/cgo/ is how to call c from go. A non-trivial use of cgo is https://github.com/mattn/go-sqlite3.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.