Go Builds and Tests
Build, run, test, cross-compile, and cache Go packages with Struo, including
vendored modules, cgo, and exported C libraries declared in struo.toml.
Struo models Go packages, executables, and tests as typed targets and invokes the Go command directly. Vendored modules become queryable graph targets, and ordinary builds stay offline.
Prerequisites#
Install the pinned Go toolchain through mise:
mise install
mise exec -- go versionDeclare targets#
Declare a module#
Create struo.toml beside go.mod:
[[target]]
name = "GoDependencies"
kind = "go_dependencies"
srcs = ["go.mod", "go.sum", "vendor/modules.txt"]
[target.attrs]
resolver_inputs = ["go.mod", "go.sum", "vendor/modules.txt"]
[[target]]
name = "Greeting"
kind = "go_library"
srcs = ["internal/greeting/*.go"]
deps = ["GoDependencies"]
[target.attrs]
package = "./internal/greeting"
importpath = "example.com/hello/internal/greeting"
[[target]]
name = "Hello"
kind = "go_binary"
srcs = ["cmd/hello/*.go"]
deps = ["Greeting", "GoDependencies"]
[target.attrs]
package = "./cmd/hello"
[[target]]
name = "GreetingTests"
kind = "go_test"
srcs = ["internal/greeting/*_test.go"]
deps = ["GoDependencies"]
[target.dependencies]
target_under_test = ["Greeting"]
[target.attrs]
package = "./internal/greeting"
coverage = true
labels = ["unit"]go_source groups files that another target embeds into the same package;
connect it through the embed dependency role. Native dependencies used by
cgo belong under the cdeps role.
Query before building#
Inspect the graph before executing it:
struo query target-kinds
struo query schema go_binary
struo query targets --kind go_module
struo query capabilities HelloBuild, run, and test#
Use the shared capability commands:
struo build Hello
struo run Hello
struo test GreetingTestsThe test target compiles one Go test binary and writes
JavaScript Object Notation events, a
readable log, and normalized test results. Exact test names from
struo query test-manifest can be scheduled independently. With coverage
set, a Go coverage profile is written alongside the results.
Start from the runnable starter#
The go-comprehensive starter includes a first-party package, an embedded
source group, a runnable executable, a Linux cross-compiled executable, exact
tests with coverage, and the vendored github.com/pkg/errors module:
struo query example go_binary go-comprehensive --format jsonManage dependencies#
Import external modules#
Go stays authoritative for version selection and source verification. Update and vendor dependencies before an ordinary Struo build:
go mod tidy
go mod vendorgo_dependencies reads the requirement graph from a Go 1.17 or newer go.mod,
checks the selected versions against go.sum and vendor/modules.txt, and
emits one go_module target for every vendored module that contributes a
package. The committed vendored source is part of every consuming action's
cache key.
Build and test actions run with -mod=vendor, GOPROXY=off, and
GOSUMDB=off, so they cannot fetch missing source. A missing checksum, a
version disagreement, or a vendored module absent from the requirement graph
fails while the graph loads.
For a go.work workspace, set workspace_file, include that file and every
relevant checksum file in sum_files and resolver_inputs, and create the
vendor tree with go work vendor. Consumers set go_work to the same
workspace-relative file.
Cross-compile and export native libraries#
Set goos and goarch on a binary or library to select its destination:
[[target]]
name = "HelloLinux"
kind = "go_binary"
srcs = ["cmd/hello/*.go"]
deps = ["Greeting", "GoDependencies"]
[target.attrs]
package = "./cmd/hello"
goos = "linux"
goarch = "amd64"
strip = trueBuild tags, compiler, assembler, and linker flags, the race detector,
sanitizers, profile-guided optimization, and link-time string definitions are
also typed attributes. See the go_binary reference.
Set build_mode to c-archive or c-shared to expose a Go main package to
native consumers. Struo returns the generated header and library as a C
provider, including the Android
Application Binary Interface
record when android_abi is set. Other supported build modes are exe, pie,
plugin, shared, and archive.
Limitations#
struo runandstruo testrequire a binary for the host operating system and architecture. Cross-compiled executables can be built but not run.- Cross-platform cgo requires destination-compatible
ccandc++commands. - Builds never fetch modules. Vendor dependencies with
go mod vendororgo work vendorfirst.
Target kinds#
Next steps#
- Linting: run golangci-lint as a cached graph target.
- Testing and Scheduling: select and schedule exact Go tests across the graph.
- Caching: share build results across machines with a remote cache.