Go is the language behind much of modern DevOps tooling, including Docker, Kubernetes, Terraform and Prometheus, because it builds fast, produces one static binary and has strong concurrency and networking support. You can use it to write CLIs, automation, Kubernetes operators, exporters and infrastructure code, and to build and ship your own services reliably.
This guide covers why Go fits infrastructure work, how to write a command-line tool, how to talk to Docker and Kubernetes, how to expose metrics, and the practices that keep infrastructure code safe.
| Strength | Why it helps operations |
|---|---|
| Single static binary | Copy it to any host; no interpreter or dependencies to install |
| Cross-compilation | Build for Linux, macOS and Windows from one machine |
| Goroutines | Poll, watch and serve many things at once cheaply |
| Standard library | HTTP, TLS, JSON, os/exec, net and context are built in |
| Fast compile and start | Short CI times, quick CLI and container startup |
| Strong typing | Catches errors before they reach production |
Tools such as Terraform providers, Kubernetes controllers, Helm and Prometheus exporters all have mature Go libraries and examples to follow.
Many operations tasks are CLIs. The standard flag package is enough for small tools; use Cobra when you need subcommands. This example checks whether a URL is healthy and returns a non-zero exit code on failure, which makes it usable in scripts and CI:
Note that os.Exit skips deferred calls, so keep the exit logic at the end, as done here. Build it with CGO_ENABLED=0 go build -o healthcheck . and ship the single file.
The Docker and Kubernetes clients are Go libraries. This program lists pods in the default namespace with client-go:
Inside a cluster, use rest.InClusterConfig() instead of a kubeconfig file. For controllers and operators, build on controller-runtime or Kubebuilder rather than polling the API by hand; they provide watches, caches, leader election and reconciliation loops.
Use os/exec to call other tools, pass arguments separately instead of through a shell, and bound execution with a context:
Never build a shell string from untrusted input. Prefer a native Go library over shelling out when one exists, since you get typed errors and no dependency on a binary being installed.
Services you operate need logs and metrics. Use log/slog for structured logs and the Prometheus client library for metrics:
Keep label values bounded. Using raw URLs or user IDs as labels creates unbounded series and can overload Prometheus. Add net/http/pprof on an internal port for CPU and memory profiling.
context.Context through every call that does I/O and set timeouts.fmt.Errorf("...: %w", err) so callers can use errors.Is and errors.As.SIGTERM with signal.NotifyContext and http.Server.Shutdown.testing package and go test -race, and lint with go vet and golangci-lint.-trimpath and a pinned Go version; publish release binaries with GoReleaser.Static binaries, easy cross-compilation and good concurrency make tools simple to distribute and fast to run. A strong standard library for networking lowers dependency counts.
Use Bash for short glue scripts. Choose Go when the tool needs tests, concurrency, structured error handling, API clients or distribution to other teams.
Use Kubebuilder or Operator SDK, which scaffold a project on controller-runtime. You define a custom resource and implement a Reconcile function that moves the cluster toward the desired state.
Load them at runtime from environment variables injected by the platform or from a secrets manager. Do not hard-code them or log them.
Go is a practical choice for DevOps and infrastructure work: it produces portable binaries, has first-class Docker and Kubernetes libraries, and handles concurrent I/O well. Write CLIs with flag or Cobra, use client-go and controller-runtime for clusters, expose logs with slog and metrics with Prometheus, and follow practices such as contexts, idempotency, graceful shutdown and CI-built release binaries.