Random software: gocairo

September 16, 2015

For another random project I needed bindings to the Cairo graphics library from Go. Looking online found a few incomplete attempts — including, embarrassingly, one that I myself had made one years ago that I had entirely forgotten about!

I decided to fix this, and published gocairo — the definitive Cairo golang bindings. (When I published this I also eliminated the incomplete prior implementation I had published, so I feel I am not contributing to a proliferation of incompatible implementations.)

This wrapper is perhaps interesting in its approach. Rather than write out a function-by-function wrapper, I instead used a C parser to parse the existing C header and then generate the wrapper from that header in code. This is similar to the SWIG approach; but because this code is a one-off program specifically for this one translation task, I can special-case as much as necessary to make the resulting API pleasant and idiomatic.

The resulting API has nearly perfect coverage of the C API, and the few remaining missing pieces are printed out as warnings during the build process.

(This project was part of a yak shave for another project that hopefully will be more interesting and worth posting about soon!)