Zig Examples
These examples pin Zig 0.16.0. Replace it deliberately when the project changes toolchain versions. No global Zig installation is needed.
Run Zig Directly
load("@[email protected]", "zig")
load("@os@1", "run")
run(("zig", "version"), deps=[zig])
zig puts the managed executable directory first in PATH.
Build a Go Binary with Zig C/C++ Compilers
load("@[email protected]", "go_binary")
load("@[email protected]", "zig_cc")
go_binary(
entry_point="cmd/server",
output="bin/server",
deps=[zig_cc("x86_64-linux-gnu")],
)
zig_cc sets both CC and CXX to managed Zig compiler commands and adds Zig to PATH. The target must be valid for Zig. This example configures the C and C++ target only; it does not set Go's GOOS or GOARCH. It works directly when the Go build target already matches, such as a Linux x86-64 host or build environment.
For true Go cross-compilation, set matching Go target variables and enable CGO:
load("@[email protected]", "go_binary")
load("@os@1", "env_set")
load("@[email protected]", "zig_cc")
go_binary(
entry_point="cmd/server",
output="bin/server",
deps=[
zig_cc("x86_64-linux-gnu"),
env_set("GOOS", "linux"),
env_set("GOARCH", "amd64"),
env_set("CGO_ENABLED", "1"),
],
)
Dependencies apply in order: zig_cc configures managed compilers and PATH, then the environment dependencies add Go's matching target settings. Project code and dependencies must also support the destination platform. For a portable host build, use zig_cc() to omit -target and use Zig's host default.