tezvyn:

Difference between go build and go install? Cross-compile for ARM64 Linux?

AI-drafted, machine-checkedSource: pkg.go.devintermediate

Tests Go toolchain artifact placement and native cross-compilation. A strong answer distinguishes go build (current directory) from go install ($GOBIN), then sets GOOS=linux GOARCH=arm64 for cross-compilation.

WHAT THIS TESTS: The interviewer wants to see if you understand where the Go toolchain places compiled artifacts and whether you know that Go supports first-class cross-compilation through environment variables alone. This is a practical workflow question: senior engineers should know how to produce binaries for deployment targets without relying on CI magic or containers.

A GOOD ANSWER COVERS: First, articulate the output difference: go build compiles the package in the current directory and leaves the resulting binary there, or at the path given by the -o flag, while go install compiles the package and then moves the binary to GOBIN, or GOPATH/bin if GOBIN is unset. Second, note that go install is the idiomatic way to install a command so it is available on your PATH, whereas go build is for local development or producing artifacts for packaging. Third, for cross-compilation, explain that you set the GOOS and GOARCH environment variables to match the target platform, for example GOOS=linux GOARCH=arm64 go build, which produces a 64-bit ARM Linux binary from any host that runs the Go toolchain. Fourth, mention that because the Go compiler is inherently cross-compiling, you do not need an external cross-compiler, linker, or Docker container for pure Go code; the standard library is built for the target automatically.

COMMON WRONG ANSWERS: A major red flag is claiming that go install merely downloads dependencies or that it is identical to go build except for naming. Another red flag is asserting that cross-compilation requires installing gcc-arm-linux-gnueabihf, QEMU, or a dedicated build container. While Docker can standardize builds, it is not a requirement for Go cross-compilation, and insisting otherwise reveals a gap in toolchain knowledge. Also, confusing the legacy pre-modules behavior of go get with the modern go install command is a signal that the candidate has not kept up with recent toolchain changes.

LIKELY FOLLOW-UPS: The interviewer might ask how to cross-compile when using CGO, which does require a target C toolchain because the Go compiler must invoke a cross-compiling C compiler. They might also ask where the binary lands if GOBIN is not set, or how go install behaves with a version suffix like go install example.com/cmd@latest. Another common follow-up is how to verify the architecture of a produced binary, which can be done with the file command on Unix systems or by reading the build info.

ONE CONCRETE EXAMPLE: Imagine you are on a MacBook with an Apple Silicon processor and you need to deploy a service to an AWS Graviton instance running Amazon Linux. You would run GOOS=linux GOARCH=arm64 go build -o myservice in your project root. This produces a statically linked binary named myservice that you can scp to the server and run immediately. If you were instead iterating locally and wanted the tool available in your shell, you would run go install, which places the binary in $GOBIN/myservice so you can invoke it without a relative path.

Read the original → pkg.go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.