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:
- 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. - **Direct Linking via
.syso**
The Go compiler (cmd/compileandcmd/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.sysointo 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/R9on Windows orRDI/RSI/RDX/RCXon 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=0builds 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!