Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
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.
| Item | Value |
|---|---|
| Official release | tarball from go.dev/dl → /usr/local/go |
| Packages | RHEL: golang (Go Toolset); Ubuntu: golang-go (often older) |
| Tools in your PATH | /usr/local/go/bin, $HOME/go/bin (tools you go install) |
| Settings | go env (stored in ~/.config/go/env) |
| Caches | ~/go/pkg/mod (modules), ~/.cache/go-build (builds) |
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.
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/"| Setting | Why |
|---|---|
GOPRIVATE | Private modules skip the public proxy and checksum DB (no code names leak). |
GOPROXY | Your company proxy (Nexus/Artifactory/Athens) first, then the public one; direct as fallback. |
insteadOf | Fetch 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:
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| Option | Why |
|---|---|
CGO_ENABLED=0 | A static binary — no glibc needed, runs in scratch/distroless. |
-trimpath | No local paths in the binary (reproducible, no info leak). |
-ldflags "-s -w" | Smaller binary (no debug symbols). -X sets a version variable. |
GOOS/GOARCH | Cross-compile for other systems from one machine. |
govulncheck | Finds known vulnerabilities that your code really calls. |
A tiny, secure container image:
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).
Make it safe
Security checklist:
- Commit
go.modandgo.sum;-mod=readonlyin CI. govulncheckin CI; update dependencies regularly (go get -u+ tests).GOPRIVATEfor 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.
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.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
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 repo | The proxy can not see private code. | Set GOPRIVATE; configure Git credentials (insteadOf, SSH key or .netrc). |
verifying …: checksum mismatch | The 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 available | Offline 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 directory | It was built with cgo (dynamic). | Build with CGO_ENABLED=0. |
x509: certificate signed by unknown authority in a scratch image | No CA certificates in the image. | Use distroless (has CA certs) or copy /etc/ssl/certs/ca-certificates.crt. |
Next steps
- Command page: go.
- At work: Go — the language of cloud-native tools.
- Build the image: Buildah or Docker. Scan it: Trivy. Store modules: Nexus.
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.