هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
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.
runFILE.go|.- Build and run at once.
test./...- Run all tests.
modinitNAME/modtidy- Start a module / clean go.mod and go.sum.
getPKG@VERSION- Add or change a dependency.
installPKG@VERSION- Install a tool into ~/go/bin.
vet./.../fmt./...- Find suspicious code / format code.
env[VAR]- Show Go settings (GOPATH, GOOS …).
version/list-mall- Go version / all modules in use.
الخيارات 9
الأكثر استخدامًا
Build and test options
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 11
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Start a project
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 .3Test everything with race checks and coverage
go test -race -cover ./...4Build for a Raspberry Pi / ARM server
GOOS=linux GOARCH=arm64 go build -o bin/app-arm64 .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"]6Add and update a dependency
go get github.com/prometheus/client_golang@v1.20.5 go mod tidy7Install a Go tool
go install github.com/google/go-containerregistry/cmd/crane@latest8Check code before a commit
go vet ./... && test -z "$(gofmt -l .)"9Check dependencies for known vulnerabilities
go install golang.org/x/vuln/cmd/govulncheck@latest govulncheck ./...10Run one test, verbose, no cache
go test -run TestHealth -v -count=1 ./internal/api11Where does Go keep things?
go env GOPATH GOMODCACHE GOOS GOARCH
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Commit both
go.modandgo.sum;go.sumchecks 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/modand~/.cache/go-buildin CI for fast builds.
رموز الخروج
0 | OK. |
1 | Build or test error. |
2 | Usage error. |