الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

go

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

المستوى
أداة DevOps
المجموعة
أدوات البناء ولغات البرمجة
يحتاج root
لا
الحزمة
golang

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

go COMMAND [OPTIONS] [PACKAGES]

الأجزاء الرئيسية

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.

الخيارات 9

الأكثر استخدامًا

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.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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

انتبه

  • 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.

الملفات

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

نصائح للحفظ

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

رموز الخروج

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

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man go