Build and distribute service binaries
Build and distribute service binaries
Build a service for its actual operating system and CPU architecture. GOOS and GOARCH select the target; CGO_ENABLED=0 disables cgo, which may change dependency availability and behavior. Pin a supported Go toolchain and dependencies, test before building, and record checksums and build settings. Cross-compilation is not native execution testing.
package main
import ( "fmt")
func supported(os, arch string) bool { return os == "linux" && (arch == "amd64" || arch == "arm64") }func main() { fmt.Println(supported("linux", "amd64"), supported("linux", "arm64"), supported("windows", "amd64"))}This support matrix is an example release policy, not the complete list of Go targets. Use go tool dist list for actual supported pairs. Build a pure-Go module with CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath -o service-linux-arm64 .; use a native ARM runner for execution. Do not assume cgo-dependent applications will build this way.
Expected output:
true true falseInspect build information
package main
import ( "fmt" "runtime" "runtime/debug")
func main() { info, ok := debug.ReadBuildInfo() fmt.Println(ok, info != nil) fmt.Println(runtime.GOOS != "", runtime.GOARCH != "")}Build metadata can help identify the toolchain and module versions. Use go version -m on a released binary. -trimpath removes filesystem paths from the compiled executable, but is not a full reproducibility guarantee. Distribute immutable artifacts with an authenticated manifest, and test DNS, CA roots and filesystem assumptions in the target environment.
Expected output:
true truetrue trueTry it locally
Create names for Linux amd64 and arm64 release artifacts from OS and architecture strings. Print service-linux-amd64 and service-linux-arm64.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service protocols and cloud 04: Build and distribute service binaries
Check the lesson objective before moving on.
What does a successful cross-build establish?
What does -trimpath alone guarantee?