---
title: Zig Examples
description: Direct Zig commands and Go CGO builds with a managed Zig distribution.
---

# 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

```python
load("@zig@0.16.0", "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

```python
load("@go@1.26.5", "go_binary")
load("@zig@0.16.0", "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:

```python
load("@go@1.26.5", "go_binary")
load("@os@1", "env_set")
load("@zig@0.16.0", "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.

<!--
Sitemap

URL: https://withxx.dev/index.md
Title: xx
Description: Practical, reproducible builds without build-system ceremony

URL: https://withxx.dev/examples/commands.md
Title: Command Examples
Description: General command, environment, and entrypoint patterns.

URL: https://withxx.dev/examples/go.md
Title: Go Examples
Description: Common Go build patterns with managed SDKs.

URL: https://withxx.dev/examples/nodejs.md
Title: Node.js Examples
Description: Common Node.js task patterns with a managed distribution.

URL: https://withxx.dev/examples/zig.md
Title: Zig Examples
Description: Direct Zig commands and Go CGO builds with a managed Zig distribution.

URL: https://withxx.dev/load.md
Title: Loading Files
Description: Split xx configuration into local Starlark modules.

URL: https://withxx.dev/packages/go.md
Title: Go Package
Description: Provision an official Go SDK, run or install Go tools, and build Go binaries.

URL: https://withxx.dev/packages/nodejs.md
Title: Node.js Package
Description: Provision official Node.js distributions and run npm and npx tasks.

URL: https://withxx.dev/packages/os.md
Title: OS Package
Description: Run commands and construct isolated task environments.

URL: https://withxx.dev/packages/zig.md
Title: Zig Package
Description: Provision an official Zig distribution and configure Zig as a C and C++ compiler.
-->
