draffensperger / go-interlang

Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GO binding struct for a self referencing struct

vposham opened this issue · comments

Hi,

Any ideas on how to write a golang equivalent struct for self referencing struct something like this -

typedef struct WRITER {
int version;
int (WriteBlock)(struct WRITER thisPtr,
const void* dataBufferPtr,
unsigned long bufferSize);
} WRITER;

Thanks.

Given that cgo allows you to reference C types via something like C.WRITER, in this case would it make sense to just reference the C types directly? Then maybe you could have a Go wrapper for a higher level concept in your program such as the thing that does the writing using the underlying struct.

Disclaimer : Im a C noob who just came to know that - there is something like self referencing structs.

I tried directly referencing C struct like C.WRITER, but the WriteBlock method implementation is required to use the object in a meaningful way.

To give more context - Im trying to write a Go struct wrapper for FPDF_FILEWRITE_ struct to use in FPDF_SaveAsCopy function.

https://pdfium.googlesource.com/pdfium/+/refs/heads/master/public/fpdf_save.h

Digging further, I came to know that something like this needs to be implemented in GO.