Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

go

Build, test and run Go programs — the language of Docker, Kubernetes, Terraform and most cloud-native tools.

Level
DevOps tool
Group
Build tools and languages
Needs root
No
Package
golang

What it does

The go command does it all: download modules (go.mod), build a binary, run tests, check code. A Go program builds into one static binary with no runtime, so container images can be tiny (FROM scratch or distroless).

DevOps engineers use go to build internal tools, Kubernetes operators and exporters, and to read and fix the code of the tools they run.

How to write it

go COMMAND [OPTIONS] [PACKAGES]

Main parts

Commands

build [-o FILE] [PKG]
Compile a binary.
run FILE.go | .
Build and run at once.
test ./...
Run all tests.
mod init NAME / mod tidy
Start a module / clean go.mod and go.sum.
get PKG@VERSION
Add or change a dependency.
install PKG@VERSION
Install a tool into ~/go/bin.
vet ./... / fmt ./...
Find suspicious code / format code.
env [VAR]
Show Go settings (GOPATH, GOOS …).
version / list -m all
Go version / all modules in use.

Options 9

Most used

Build and test options

-o FILE
Name of the output binary.
-ldflags "-s -w -X main.version=1.4.0"
Smaller binary; set a variable at build time.
-trimpath
Remove local paths from the binary (reproducible builds).
-race
Find data races (tests).
-cover / -coverprofile=c.out
Test coverage.
-run REGEX / -v
Only matching tests / show each test.
-count=1
Do not use cached test results.
CGO_ENABLED=0
Build a fully static binary.
GOOS=linux GOARCH=arm64
Build for another OS / CPU.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Start a project

    mkdir hello && cd hello
    go mod init github.com/acme/hello
    go run .
  2. 2Build a small static binary with a version

    CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=$(git describe --tags)" -o bin/app .
  3. 3Test everything with race checks and coverage

    go test -race -cover ./...
  4. 4Build for a Raspberry Pi / ARM server

    GOOS=linux GOARCH=arm64 go build -o bin/app-arm64 .
  5. 5A tiny image with a static binary

    cat Dockerfile
    FROM golang:1.23 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 /app .
    FROM gcr.io/distroless/static:nonroot
    COPY --from=build /app /app
    ENTRYPOINT ["/app"]
  6. 6Add and update a dependency

    go get github.com/prometheus/client_golang@v1.20.5
    go mod tidy
  7. 7Install a Go tool

    go install github.com/google/go-containerregistry/cmd/crane@latest
  8. 8Check code before a commit

    go vet ./... && test -z "$(gofmt -l .)"
  9. 9Check dependencies for known vulnerabilities

    go install golang.org/x/vuln/cmd/govulncheck@latest
    govulncheck ./...
  10. 10Run one test, verbose, no cache

    go test -run TestHealth -v -count=1 ./internal/api
  11. 11Where does Go keep things?

    go env GOPATH GOMODCACHE GOOS GOARCH

Watch out

  • Commit both go.mod and go.sum; go.sum checks that downloads are not changed.
  • With CGO_ENABLED=1 (default on Linux) a binary can need glibc and fail on Alpine or scratch.

Files

go.mod / go.sum
Module name, dependencies and their checksums.
~/go/bin/
Tools from go install.

Tips to remember

  • Cache ~/go/pkg/mod and ~/.cache/go-build in CI for fast builds.

Exit codes

0OK.
1Build or test error.
2Usage error.