← 返回命令列表

Linux command

go-build 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Build current package

go build

Build specific file

go build [main.go]

Set output name

go build -o [binary-name]

Cross-compile

GOOS=[linux] GOARCH=[amd64] go build

Build with race detector

go build -race

Build with trimmed paths

go build -trimpath -o [binary-name]

Build with profile-guided optimization

go build -pgo=[profile.pprof] -o [binary-name]

说明

go build compiles Go packages and dependencies. It produces an executable binary from the main package or checks compilation for library packages. The command handles dependency resolution, compilation, and linking. Cross-compilation is built-in via GOOS and GOARCH environment variables, requiring no additional toolchains.

参数

-o _FILE_
Output file name.
-v
Verbose output.
-race
Enable race detector.
-ldflags _FLAGS_
Linker flags.
-tags _TAGS_
Build tags.
-trimpath
Remove all file system paths from the resulting executable.
-gcflags _'pattern=arg list'_
Arguments passed to the Go compiler.
-mod _MODE_
Module download mode: readonly, vendor, or mod.
-pgo _FILE_
Profile-guided optimization file (default: auto).
-cover
Enable code coverage instrumentation.
-a
Force rebuilding of packages already up-to-date.
-n
Print commands but do not run them.
-x
Print the commands as they are executed.
-buildmode _MODE_
Build mode (default, archive, c-archive, c-shared, shared, exe, pie, plugin).

FAQ

What is the go-build command used for?

go build compiles Go packages and dependencies. It produces an executable binary from the main package or checks compilation for library packages. The command handles dependency resolution, compilation, and linking. Cross-compilation is built-in via GOOS and GOARCH environment variables, requiring no additional toolchains.

How do I run a basic go-build example?

Run `go build` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -o _FILE_ do in go-build?

Output file name.