- go build - 编译 Go 源代码为可执行文件或库文件。
go build main.go
- go run - 编译并直接运行 Go 源代码。
go run main.go
- go install - 编译并安装指定的包或命令,保存在 $GOPATH/bin 目录下。
go install example.com/myapp
- go get - 下载并安装指定的第三方库,保存在 $GOPATH/pkg 中。
go get github.com/gin-gonic/gin
- go test - 运行测试文件,并输出测试结果。
go test -v example_test.go
- go vet - 检查程序的常见错误。
go vet main.go
- go fmt - 格式化 Go 代码。
go fmt main.go
- go mod init - 初始化一个新的 Go 模块。
go mod init example.com/myapp
- go mod tidy - 整理和升级依赖项,自动清理不需要的依赖项。
go mod tidy
- go mod vendor - 将依赖项复制到项目的 vendor 目录下,并生成相应的 go.mod 和 go.sum 文件。
go mod vendor
- go doc - 查看 Go 中的文档。
go doc fmt.Printf
- go generate - 运行指定目录下的所有 go:generate 指令。
go generate ./...
- go list - 列出指定的包或模块。
go list -json all
- go clean - 清理对象文件和可执行文件。
go clean -i
- go version - 查看当前 Go 的版本信息。
go version
- go env - 显示 Go 环境变量信息。
go env
- go test -cover - 输出 Go 程序的代码覆盖率。
go test -cover -v ./...
- go tool pprof - 生成 CPU 和内存分析报告。
go tool pprof http://localhost:8080/debug/pprof/profile
- go install -race - 编译并安装带有数据竞争检测的包。
go install -race example.com/myapp
- go build -ldflags - 设置构建期间的标志变量。
go build -ldflags="-X main.version=1.0.0" main.go
- go doc -all - 显示 Go 中所有的文档。
go doc -all fmt
- go tool vet - 运行 Go 工具链中的静态代码分析工具。
go tool vet main.go
- go test -bench - 运行性能测试。
go test -bench=. example_test.go
- go test -run - 运行指定的测试用例。
go test -run TestExample example_test.go
- go get -u - 更新指定的包或命令。
go get -u example.com/myapp
- go install -tags - 标记指定的包或命令。
go install -tags=json example.com/myapp
- go list -deps - 列出指定包的所有依赖。
go list -deps example.com/myapp
- go tool trace - 生成程序的运行时跟踪信息。
go run -race main.go 2>&1 | tee trace.out
go tool trace trace.out
- go test -short - 运行短时测试。
go test -short .
- go build -o - 将编译后的程序另存为指定文件名。
go build -o myapp.exe main.go
- go tool cover -func - 显示代码覆盖率报告。
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
- go mod download - 下载所有依赖项,但不安装它们。
go mod download
- go vet -json - 将检测结果以 JSON 格式输出。
go vet -json main.go
- go fmt -s - 对代码进行简化。
go fmt -s main.go
- go list -e - 显示错误信息。
go list -e example.com/myapp
- go tool pprof -http - 生成交互式的 CPU 或内存分析报告。
go tool pprof -http=:8080 http://localhost:8080/debug/pprof/profile
- go env -json - 将环境变量以JSON形式输出。
go env -json
- go test -covermode - 设置代码覆盖分析模式。
go test -covermode=count example_test.go
- go mod vendor -n - 打印哪些文件被拷贝到 vendor 目录中。
go mod vendor -n
- go mod why - 解释为什么需要使用这个包。
go mod why github.com/gin-gonic/gin
- go version -m - 查看模块和版本信息。
go version -m
- go tool trace -http - 将生成的跟踪数据转换为可视化报告并通过 HTTP 服务器提供。
go run -race main.go 2>&1 | tee trace.out
go tool trace -http=:8080 trace.out
- go clean -i -r - 清理项目中的所有依赖项。
go clean -i -r example.com/myapp
- go list -json - 显示指定包或模块的详细信息。
go list -json example.com/myapp
- go build -gcflags - 设置编译器的标志变量。
go build -gcflags="-N -l" main.go
- go vet -tests - 检查测试文件中的错误。
go vet -tests .
- go test -v -verbose - 显示更多详细信息。
go test -v -count=1 ./...
- go tool tour - 在本地启动 Go Tour 服务。
go tool tour
- go mod edit - 编辑 go.mod 文件。
go mod edit -replace=example.com/old/example=example.com/new/example
- go test -cpu - 设置测试使用的 CPU 数量。
go test -cpu=1,2,4 example_test.go
以上是 50 条常用的 Go 命令及使用案例,希望对你有所帮助。