How we achieved single-binary static linking without Cgo (Windows & Linux)

Hi, fellow Gophers!

I’m excited to share a project I’ve been working on: a completely Cgo-free Go binding for the Slint UI engine, currently working on both Windows and Linux (amd64).

For years, building native desktop GUIs in Go has forced developers into difficult trade-offs: either pull in a heavy web stack (Chromium/Webview via Wails), deal with immediate-mode UI quirks, or wrestle with Cgo dependencies that break fast build times and painless cross-compilation.

Slint is a modern, declarative GUI toolkit written in Rust with tiny resource footprints, beautiful reactive DSL, and a royalty-free desktop license for proprietary apps. However, bringing a Rust engine into Go without Cgo seemed nearly impossible at first. Here is how it is built under the hood.


The Technical Challenge: Why Zero-Cgo is Hard

In standard Go development, linking foreign native libraries requires Cgo. To eliminate Cgo entirely, two major hurdles had to be solved:

  1. Self-contained Native Static Library
    Cgo cannot be removed if the build process still invokes C compiler headers. We bundled the entire Slint engine and its C-API wrapper (SlintCFlat) into a single, precompiled static archive.
  2. **Direct Linking via .syso**
    The Go compiler (cmd/compile and cmd/link) natively recognizes .syso (System Object) files placed inside a package directory and links them directly without invoking GCC or Clang. But simply dropping a .syso into the package does not work out of the box.

The Secret Sauce: Bridging the ABI Gap with Trampolines

The core reason .syso linking fails for foreign functions is the ABI mismatch:

  • Go uses its own internal calling convention (historically Plan 9 ABI on x86, transitioning to register-based ABI0/ABIInternal).
  • This convention is fundamentally incompatible with standard OS calling conventions (Microsoft x64 ABI on Windows, System V AMD64 ABI on Linux).
  • Register allocations, stack alignment, and shadow space differ completely.

To make the Go compiler call into Slint’s C-API directly:

  • Every single API function requires an assembly trampoline that saves the Go runtime state, aligns the stack according to the host platform’s ABI, moves parameters from Go’s registers/stack into host registers (e.g., RCX/RDX/R8/R9 on Windows or RDI/RSI/RDX/RCX on Linux), calls the foreign symbol, and marshals return values back to Go.
  • Writing hundreds of assembly trampolines by hand across multiple OS targets is virtually impossible to maintain.

To solve this, the trampolines and Go function stubs are automatically generated by the Hike language compiler (a toolchain I developed). You don’t have to write a single line of assembly or C.


The Result

By combining pre-packaged .syso archives with compiler-generated assembly trampolines, we achieve:

  • Zero Cgo: CGO_ENABLED=0 builds just work.(However, you must manually specify the options that the Slit package passes to the linker.)
  • Lightning-fast compilation: Instant builds using pure Go toolchains.
  • Single static binaries: Slint markup files are merged via our bundling tool and embedded directly into the binary using go:embed.

Currently, the binding uses the Slint dynamic interpreter to evaluate UI markup at runtime. We hope that one day the Slint team will provide first-class native Go code generation, but for now, this provides a fast, lightweight, and modern GUI experience for Go developers.

Check out the repository and examples here:

Feedback, questions about the linking internals, and suggestions are very welcome!


“Go function stubs are automatically generated by the Hike language compiler (a toolchain I developed).”

Very cool stuff!

It’s also interesting to see in the project that UI events are handled in sort of queues, if I am not wrong (Reminds me of io_uring( io_uring - Wikipedia ) but used at a higher level).

Thanks for the kind words, Akilan!

The io_uring comparison is an interesting take, but the architectural reason for this explicit polling loop goes even deeper into how we manage the OS main thread.

In standard Slint bindings (like JavaScript), event handling uses callbacks where handler functions are invoked implicitly. While convenient, that approach obscures how the main thread waits for events and turns the main event loop into a black box.

In this Go binding, we intentionally chose an explicit event polling model:

  1. Deterministic thread execution: Event processing is guaranteed to run explicitly on the main thread (locked via runtime.LockOSThread()).
  2. Coexistence with main-thread-only OS resources: Since polling can return or time out when idle, developers can interleave other OS-level operations that strictly require the main thread (such as Win32 COM/OLE routines, macOS Cocoa loops, or native platform resources) alongside GUI event processing.

Giving full control of the main loop back to the developer is what makes this architecture so robust for real-world Go applications!

Thanks a lot for taking the time for writing the response! (Very much appreciated). The deterministic thread execution is really interesting.

“developers can interleave other OS-level operations that strictly require the main thread”

I see, does this mean the entire library runs on a single thread ?

Not quite! It doesn’t restrict the entire library or your application to a single thread.

Rather, only the UI event loop and OS-level windowing interactions are explicitly pinned to the OS main thread (via runtime.LockOSThread()). This is strictly to satisfy OS requirements (like Win32 message pumps, macOS Cocoa runloops, or GPU context rules) where certain resources must not migrate across native threads.

Everything else in Go behaves completely normally:

  • You can freely spawn background goroutines across multiple OS threads for heavy computation, file I/O, or networking.
  • Background goroutines can easily communicate with the UI loop using standard Go channels or safe synchronization primitives.
  • Slint’s internal engine can also manage its own background workers or rendering tasks where applicable.

In short, it guarantees thread-affinity where the OS demands it, while keeping Go’s full M:N concurrency model intact everywhere else!

I see, got it!

Thanks, Will definitely keep an eye out on the project for future updates (I am working on UI system for analysing flight data from multiple sessions of flight simulations which will be open source by the mid of next year. I have added it to my list of libraries to potentially use by early Jan 2027).

Hi Akilan,

As a quick update following our discussion on direct GPU/vector rendering needs:

To help advance the Go graphics ecosystem—particularly for game engines and custom GUI toolkits beyond Slint—I have just released WGPUStatic, a Cgo-free static binding of Rust’s wgpu-native for Go:

It enables linking wgpu-native directly into standalone Go executables via .syso and Plan 9 assembly stubs without runtime DLL dependencies (currently tested and running on Windows amd64).

Since your flight simulation telemetry and analysis project will likely require high-performance, raw GPU rendering pipelines alongside UI widgets, I thought this might be useful for your architectural considerations. Best of luck with your project!