go_binary
go_binary builds one Go source file or one package directory with the selected managed SDK. It registers the build as a root task and returns its dependency.
Arguments
| Argument | Required | Description |
|---|---|---|
entry_point | Yes | Project-relative Go source file or package directory passed to go build. |
deps | No | Dependencies activated before the build, in list order. Defaults to []. |
output | No | Project-relative output file. Defaults to .xx/out/<entrypoint-name>. |
flags | No | String arguments inserted after go build and before xx's output and entrypoint arguments. Defaults to []. |
The selected SDK is implicit; do not add go to deps. A source file uses its filename without extension for the default output. A package directory uses its directory name, while entry_point="." uses the project directory name. Windows adds .exe to default and custom outputs unless the name already ends with .exe, case-insensitively. xx creates missing parent directories.
Use output rather than -o or --o in flags. Entrypoints and outputs must remain inside the project after symlinks are resolved. Outputs cannot replace entrypoints, name directories, or overlap another active output.
Examples
Build one source file
load("@[email protected]", "go_binary")
go_binary(entry_point="main.go")
This writes .xx/out/main, or .xx/out/main.exe on Windows.
Build a package to a chosen path
load("@[email protected]", "go_binary")
go_binary(
entry_point="cmd/server",
output="bin/server",
)
Build a reproducible static binary
load("@[email protected]", "go_binary")
load("@os@1", "env_set")
go_binary(
entry_point="cmd/server",
output="dist/server",
flags=["-trimpath", "-ldflags=-s -w"],
deps=[env_set("CGO_ENABLED", "0")],
)