CGO-free Go bindings

purego bindings

CGO-free Go bindings by metal3d

Go bindings for system libraries that avoid cgo. They call into C at runtime through github.com/ebitengine/purego, so the Go toolchain builds your program like any other Go program.

$ go get metal3d.org/purego/gtk4@v0.8.0
go: added metal3d.org/purego/gtk4 v0.8.0
$ go build ./cmd/app
✓ built — gcc was never invoked
# GTK4 is loaded at runtime
The technique

What is purego?

purego is a library that lets Go code call C functions without cgo. At startup it opens a shared library with dlopen, resolves the symbols it needs, and takes care of the platform ABI when calling them. The binding packages on this site build on that mechanism.

The result is a normal Go package: no C compiler, no C headers, no pkg-config and no cgo at build time. For the GTK, GLib, Pango and GStreamer bindings, GIR definitions drive the generator; irregular APIs are wrapped by hand. Other libraries can be bound from a different API description or written by hand.

Your code imports idiomatic Go types and functions. The C library is still there — it is simply loaded when the program runs instead of being linked when the program is built.

  • No C compiler
  • No dev headers
  • No pkg-config
  • No cgo build tags
Motivation

Why these bindings?

cgo is powerful, but it turns every consumer of a library into a C-toolchain problem. These bindings move that complexity out of the build.

Lighter builds

No C toolchain, no development headers, no cgo build tags. If Go builds on the machine, the binding builds too — including on minimal containers.

Simple integration

Plain go get, ordinary Go modules, no special flags. No build matrix that exists only to provide different C environments.

Simpler delivery and CI

You ship a normal Go binary. Cross-compilation is easier and CI images no longer need a full set of dev packages just to compile the bindings.

Runtime requirement: the target shared libraries are still required at runtime. A program using the GTK4 bindings needs GTK4 and Adwaita, plus WebKit 6 when it uses the webkit package; the GStreamer bindings need GStreamer. This removes the build-time C dependency, not the runtime library.

Modules

Available bindings

Stable import paths under metal3d.org/purego, backed by public repositories on git.metal3d.org/purego.

metal3d.org/purego/gst

v0.3.0

GStreamer bindings: pipelines, elements, buses and the core GObject/GStreamer types, without cgo in your build.

$ go get metal3d.org/purego/gst@v0.3.0

metal3d.org/purego/gtk4

v0.8.0

GTK4, Adwaita and WebKit 6 bindings: widgets, application lifecycle, GObject signals, libadwaita and embedded web views.

$ go get metal3d.org/purego/gtk4@v0.8.0

metal3d.org/purego/<module>

soon

The purego organisation is a growing collection of Go bindings, not just these two. New modules follow the same import path and repository layout.

go get resolves these paths through the go-import metadata served on this host, then fetches the module from its public Git repository.

Examples

GTK in practice

Two programs built with gtk4-purego: a WebKit 6 web view and the GTK4 / Adwaita widget gallery.

A web browser window built with GTK4, Adwaita and WebKit 6 through gtk4-purego
WebKit 6 web view — the webkit package.
GTK4 and Adwaita widgets rendered by gtk4-purego
GTK4 and Adwaita widgets — the gtk and adw packages.

Both examples live in the gtk4-purego repository.

Architecture

How it works

From a C library description to a typed Go call, with no C compiler in the chain.

  1. API description GIR XML for the GTK, GLib, Pango and WebKit libraries; C headers, ABI data or a hand-written spec for other libraries.
  2. Binding layer Generated from GIR where it fits, hand-written for irregular APIs; the result is typed Go code with structs, enums, constants and error handling.
  3. Go package An idiomatic module such as metal3d.org/purego/gtk4, versioned with ordinary Go modules.
  4. Runtime dlopen github.com/ebitengine/purego loads the system library and binds its symbols.
  5. System library libgtk-4.so, libgstreamer-1.0.so and their dependencies, installed on the host.

Build time stays pure Go; the C library enters the picture only when the program runs.

Status

Early and evolving

These bindings are usable today, but they are not pretending to be finished.

Early v0.x releases

The APIs may still evolve between minor versions. Pin versions in production and read the release notes before upgrading.

More bindings planned

GStreamer and GTK4 are the first modules. Additional system libraries will join the purego organisation over time.

Not a cgo drop-in

Existing cgo-based bindings expose different APIs. Expect to adapt code rather than swap an import path and rebuild.

Runtime libraries required

Deployment still needs the target shared libraries on the target system. The gain is at build time, not a way to avoid installing GTK or GStreamer.