Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 96Go · Languages and build

Install and set up Go

Go is the language of the cloud-native world: Kubernetes, Docker, Terraform, Prometheus, Helm, Vault and most DevOps CLIs are written in Go. As an engineer you build Go services in CI, write small tools that compile to one static binary, and need to manage modules, private repositories and vulnerability checks. This guide installs Go from the official release (or the distro), sets up modules, proxies and private repos, builds small and secure binaries and images, and covers the common errors.

  • 8 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

96.1

Know it before you install

A module (go.mod) is a project with its dependencies and versions; go.sum holds their checksums. go build makes one binary; with CGO_ENABLED=0 it is fully static and runs in an empty (scratch/distroless) container. Dependencies come through a module proxy (default proxy.golang.org) and are checked against the checksum database.

ItemValue
Official releasetarball from go.dev/dl → /usr/local/go
PackagesRHEL: golang (Go Toolset); Ubuntu: golang-go (often older)
Tools in your PATH/usr/local/go/bin, $HOME/go/bin (tools you go install)
Settingsgo env (stored in ~/.config/go/env)
Caches~/go/pkg/mod (modules), ~/.cache/go-build (builds)

Commando's in deze les

96.2

Install the official release (RHEL and Ubuntu)

The newest version, the same on every distribution (choose it on go.dev/dl):

GOVER=1.25.4
curl -fsSLO https://go.dev/dl/go$GOVER.linux-amd64.tar.gz
sha256sum go$GOVER.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go$GOVER.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' >> ~/.bashrc
source ~/.bashrc
go version
go version go1.25.4 linux/amd64

(Compare the checksum with the one on go.dev/dl.) Distro packages are fine too: dnf -y install golang on RHEL (Go Toolset, supported by Red Hat) or apt-get install -y golang-go on Ubuntu (check that the version is new enough for your projects).

Onthoud

A go.mod can ask for a newer Go (toolchain go1.25.4); Go then downloads that toolchain by itself (GOTOOLCHAIN=auto). Set GOTOOLCHAIN=local in CI if you want to control it.

96.3

First configuration: modules, proxy, private repos

go env -w GOPRIVATE='github.com/ORG/*,gitlab.corp.example.com/*'
go env -w GOPROXY='https://proxy.golang.org,direct'
go env GOPATH GOMODCACHE GOPROXY GOPRIVATE GOFLAGS
git config --global url."git@github.com:ORG/".insteadOf "https://github.com/ORG/"
SettingWhy
GOPRIVATEPrivate modules skip the public proxy and checksum DB (no code names leak).
GOPROXYYour company proxy (Nexus/Artifactory/Athens) first, then the public one; direct as fallback.
insteadOfFetch private repos with your SSH key instead of HTTPS passwords.
GOFLAGS=-mod=readonly (CI)Builds fail if go.mod would change — reproducible builds.

A new project:

mkdir shopctl && cd shopctl
go mod init github.com/ORG/shopctl
go get github.com/spf13/cobra@v1.10.1
go mod tidy
go run .

96.4

Build, test and check

go vet ./...
go test ./... -race -cover
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=1.4.0" -o bin/shopctl .
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bin/shopctl-arm64 .
GOOS=windows GOARCH=amd64 go build -o bin/shopctl.exe .
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
go list -m -u all | head
go mod why -m github.com/some/dependency
OptionWhy
CGO_ENABLED=0A static binary — no glibc needed, runs in scratch/distroless.
-trimpathNo local paths in the binary (reproducible, no info leak).
-ldflags "-s -w"Smaller binary (no debug symbols). -X sets a version variable.
GOOS/GOARCHCross-compile for other systems from one machine.
govulncheckFinds known vulnerabilities that your code really calls.

A tiny, secure container image:

Containerfile
FROM docker.io/library/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/shop-api ./cmd/shop-api
 
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/shop-api /shop-api
USER nonroot:nonroot
ENTRYPOINT ["/shop-api"]

Real uses at work:

  • Build and test Go services and CLIs in CI; images of a few MB.
  • Write small internal tools (one binary, no runtime to install on servers).
  • Read and patch the source of the DevOps tools you use (Kubernetes controllers, exporters, Terraform providers).

96.5

Make it safe

Security checklist:

  • Commit go.mod and go.sum; -mod=readonly in CI.
  • govulncheck in CI; update dependencies regularly (go get -u + tests).
  • GOPRIVATE for internal code; a company proxy for availability and control.
  • Static, non-root, distroless images; -trimpath.
  • Keep Go updated — only the last two major versions get security fixes.

Commando's in deze les

96.6

Update and remove

Install the new tarball the same way (it replaces /usr/local/go), or dnf upgrade golang / apt upgrade golang-go. Clean caches when needed: go clean -cache -modcache. Remove: rm -rf /usr/local/go ~/go ~/.cache/go-build.

96.7

Fix common problems

ProblemCauseFix
go: command not found/usr/local/go/bin not in PATH.Add it to ~/.bashrc; open a new shell.
go: … reading https://proxy.golang.org/…: 404/410 for a private repoThe proxy can not see private code.Set GOPRIVATE; configure Git credentials (insteadOf, SSH key or .netrc).
verifying …: checksum mismatchThe module changed after it was published, or a bad cache.go clean -modcache; check the dependency; never edit go.sum by hand.
go: downloading go1.x … toolchain not availableOffline and go.mod needs a newer toolchain.Install that Go version; or GOTOOLCHAIN=local and update go.mod.
Binary fails in scratch: no such file or directoryIt was built with cgo (dynamic).Build with CGO_ENABLED=0.
x509: certificate signed by unknown authority in a scratch imageNo CA certificates in the image.Use distroless (has CA certs) or copy /etc/ssl/certs/ca-certificates.crt.

Commando's in deze les

96.8

Next steps

1How do you build a Go binary that runs in an empty container?

CGO_ENABLED=0 go build … — a static binary without glibc.

2Why set GOPRIVATE for company modules?

So private module names and code are not requested from the public proxy and checksum database.

Commando's in deze les

Alle commando's in dit hoofdstuk