liveBook · Manning

Cross-compilation

As explained in Go in Practice, Second Edition

Cross-compilation is the process of compiling software on one operating system or hardware architecture for execution on another.

Cross-compilation builds software on one operating system or hardware architecture for execution on another. Go supports this directly through the installed toolchain: set GOOS for the target operating system and GOARCH for its hardware architecture. section 12.4.2

Build a Windows binary

To build a Windows executable for the 386 architecture, run GOOS=windows GOARCH=386 go build. GOARCH can also target amd64 or arm; GOOS can target windows, linux, darwin, or freebsd. section 12.4.2

$ GOOS=windows GOARCH=386 go build

Use the compact command listing to see how target operating system and architecture are supplied to go build. section 12.4.2

For several deployment targets:

  • Use the standard Go toolchain when a straightforward build is sufficient. section 12.4.2
  • Use gox when multiple operating-system and architecture combinations should be built concurrently; its output can be organized by target. section 12.4.2
  • Test every cross-compiled result before deployment, especially when the application uses cgo and C libraries. section 12.4.2

How Go in Practice, Second Edition covers it

How deep it goes
Explains GOOS, GOARCH, gox parallelization, cgo risks, and predeployment testing, while stopping short of toolchain internals.
Examples
Uses a Windows 386 build, a gox matrix spanning Linux, macOS, and Windows, and predeployment platform testing.
What it assumes
Assumes familiarity with Go builds and introduces target selection through GOOS and GOARCH rather than platform-specific compiler internals.
What you can do after
Build Go binaries for named operating-system and architecture targets, automate parallel matrices with gox, and validate results before deployment.
How it connects
Ties cross-compilation to the Go toolchain, cgo, C libraries, Docker, and operating-system-specific application behavior.

Also explained in

Compare how 2 books explain cross-compilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.