Why Go for Tooling
Look at the tools a DevOps engineer runs every day: kubectl, helm, terraform, docker, gh, yq, k9s,
Prometheus, Grafana's agent, caddy. Almost all of them are written in Go. The reasons are practical rather than
fashionable, and they apply just as much to the small internal tools you will write:
| Property | What it means for a tool |
|---|---|
| one static binary | copy one file to a server, a container FROM scratch or a CI runner; no interpreter, no pip install, no "works with Python 3.9 but not 3.12" |
| cross-compilation built in | build Linux, macOS and Windows binaries for amd64 and arm64 from one laptop with two environment variables |
| fast start, low memory | a CLI that starts in milliseconds; an agent that idles at a few MB |
| standard library for the job | flag, encoding/json, net/http, os/exec, log/slog, context, testing cover most tools without third-party code |
| concurrency | checking 500 endpoints in parallel is a goroutine per check and a WaitGroup |
| static types | refactoring a 3,000-line tool is safe; a typo is a compile error, not a 3 a.m. surprise |
The cost is ceremony: error handling on every call, structs for every JSON shape, a build step. For a 20-line glue script that runs on one machine, Bash or Python is quicker. Bash vs Python vs Go weighs this properly. The Go course teaches the language; this section uses it to build tools.
How the examples run
Go examples run on Compiler Explorer (Go 1.25): your code is compiled and run on their servers, with no network
access and no files outside the program. Network examples use net/http/httptest, which starts a real HTTP server
on the loopback interface inside the same process: the client code is exactly what you would point at a real
API. Command-line examples pass a fixed argument list instead of reading os.Args.
A tool's skeleton
package main
import (
"fmt"
"io"
"os"
"runtime"
)
// Set at build time: go build -ldflags "-X main.version=1.4.2 -X main.commit=3f9c2d1"
var (
version = "dev"
commit = "none"
)
// run holds the whole program. It takes its arguments and output streams as parameters,
// so tests can call it with any input, and returns the exit status instead of exiting.
func run(args []string, stdout, stderr io.Writer) int {
if len(args) == 0 {
fmt.Fprintln(stderr, "usage: inventory-tool version|platform")
return 2
}
switch args[0] {
case "version":
fmt.Fprintf(stdout, "inventory-tool %s (commit %s)\n", version, commit)
case "platform":
fmt.Fprintf(stdout, "%s/%s, built with %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version())
default:
fmt.Fprintf(stderr, "unknown command %q\n", args[0])
return 2
}
return 0
}
func main() {
// A real main is one line: os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
for _, args := range [][]string{{"version"}, {"platform"}, {"deploy"}} {
code := run(args, os.Stdout, os.Stdout)
fmt.Printf("-> exit %d\n", code)
}
}inventory-tool dev (commit none)
-> exit 0
linux/amd64, built with go1.25.7
-> exit 0
unknown command "deploy"
-> exit 2
The pattern mirrors the Python main(argv) -> int: main stays trivial, and everything testable lives in run.
Call os.Exit only in main: it skips deferred functions, so calling it deep inside the program can leave files
unflushed and temporary files behind.
Building and shipping
These commands need a Go toolchain on your machine (or the build-go image of the
Go modules course), so they are shown rather than run:
# A static Linux binary with no C dependencies, version stamped in, paths and debug info stripped
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=1.4.2 -X main.commit=$(git rev-parse --short HEAD)" \
-o dist/inventory-tool ./cmd/inventory-tool
# The same for other platforms: just set GOOS and GOARCH
for target in linux/amd64 linux/arm64 darwin/arm64 windows/amd64; do
GOOS=${target%/*} GOARCH=${target#*/} CGO_ENABLED=0 \
go build -trimpath -ldflags "-s -w -X main.version=1.4.2" -o "dist/inventory-tool-${target%/*}-${target#*/}" ./cmd/inventory-tool
done
file dist/inventory-tool
# dist/inventory-tool: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, strippedCGO_ENABLED=0avoids linking against the C library, so the binary runs on any Linux (including Alpine andscratchimages). Most tools need nothing from C;netandos/userfall back to pure-Go implementations.-trimpathremoves your local file paths from the binary, which also helps reproducible builds.-ldflags "-X main.version=…"sets a string variable at link time: no generated files needed.go install example.com/tools/inventory-tool@v1.4.2builds and installs a tagged version for anyone with Go.
For releases, GoReleaser automates the matrix, archives, checksums, SBOMs and container images from one config file; see the Toolkit and the Release Engineering course.
A container for a static Go tool can be tiny:
FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" -o /out/inventory-tool ./cmd/inventory-tool
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/inventory-tool /inventory-tool
ENTRYPOINT ["/inventory-tool"]Test yourself
What does CGO_ENABLED=0 go build give you?
- A faster compiler
- A statically linked binary that does not depend on the system C library
- A debug build
- A Windows binary
How do you build a Linux arm64 binary on an amd64 laptop?
- Install an ARM computer
- Set GOOS=linux GOARCH=arm64 and run go build
- Use Docker only
- It is not possible in Go
Why does the skeleton put the program in run(args, stdout, stderr) int and call os.Exit only in main?
- Go requires it
- run can be tested with any input and output, and os.Exit skips deferred cleanup so it belongs in one place
- It makes the binary smaller
- os.Exit only works in main