13.4 Calling C Code with cgo
A Go program might need to use a hardware driver implemented in C, query an embedded database implemented in C++, or use some linear algebra routines implemented in Fortran. C has long been the lingua franca of programming, so many packages intended for widespread use export a C-compatible API, regardless of the language of their implementation.
In this section, we’ll build a simple data compression program that
uses cgo, a tool that creates Go bindings for C functions.
Such tools are called foreign-function interfaces (FFIs), and
cgo is not the only one for Go programs.
SWIG (swig.org) is another; it provides more complex
features for integrating with C++ classes, but we won’t show it here.
The compress/... ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access