How to call a go function struct with fields of type string and map from C/C++

Hello All,

Our requirement is to call a function in Golang library in C++ code. Currently we are building the library using -buildmode=c-shared .
The function in golang library is using a struct as input and output. For example like:

type Request struct {
	X  string
	Y  string
	Z  map[string]string
}

If I try using C struct then it doesn’t support string and map which makes it complex.
https://play.golang.org/p/Qjuu5j2nAv1

We have used protobuf to serialize and deserialize these complex structs. Is there any other alternative to this as it makes it harder to use this shared go src as library in another golang project.

A map is a pointer type so you could treat the map[string]string as a void * in C, then write some simple functions in Go which you call from C to do Get, Put, Keys etc which take an unsafe.Pointer, cast it to the correct map type and do the action.

2 Likes