lab/note/资源/编程/Golang/常用命令.md

101 lines
4.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 依赖与模块管理
| 命令 | 作用 | 示例 |
| ----------------- | ---------------------------------------- | ---------------------------------------- |
| `go mod init` | 初始化一个新模块(生成 go.mod | `go mod init myapp` |
| `go mod tidy` | 自动添加缺失的依赖、删除未使用的依赖 | `go mod tidy` |
| `go get` | 为当前模块添加/升级/降级依赖 | `go get github.com/gin-gonic/gin@v1.9.0` |
| `go mod download` | 预下载 go.mod 中声明的所有依赖到本地缓存 | `go mod download` |
| `go mod vendor` | 将依赖复制到 vendor 目录(离线构建用) | `go mod vendor` |
| `go list` | 列出包或模块信息 | `go list -m all`(查看所有依赖版本) |
## 运行与安装
| 命令 | 作用 | 示例 |
| :----------- | :---------------------------------- | :----------------------------------- |
| `go run` | 编译并直接运行程序(适合调试) | `go run .` |
| `go install` | 编译并安装到 GOBIN 目录(全局可用) | `go install example.com/tool@latest` |
## 代码质量与格式化
| 命令 | 作用 | 示例 |
| :------- | :--------------------------- | :------------- |
| `go fmt` | 自动格式化代码,符合官方风格 | `go fmt ./...` |
| `go vet` | 静态检查代码中潜在的错误 | `go vet ./...` |
| `go fix` | 自动把旧版 API 更新为新 API | `go fix ./...` |
## 查看信息
| 命令 | 作用 | 示例 |
| :----------- | :--------------------- | :------------------------------------- |
| `go doc` | 查看包或函数的文档 | `go doc fmt.Println` |
| `go env` | 查看/修改 Go 环境配置 | `go env -w GOPROXY=https://goproxy.cn` |
| `go version` | 查看当前 Go 版本 | `go version` |
| `go help` | 查看某个命令的详细帮助 | `go help build` |
## 其他进阶命令
| 命令 | 作用 | 说明 |
| :------------ | :---------------------------------------------- | :------------------------------------------------------- |
| `go clean` | 清除缓存和编译产物 | `go clean -cache`、`go clean -modcache` |
| `go generate` | 运行源代码中的 `//go:generate` 指令自动生成代码 | 常用于生成代码模板 |
| `go tool` | 调用 Go 自带工具 | `go tool pprof`(性能分析)、`go tool trace`(协程跟踪) |
| `go work` | 多模块工作区管理 | `go work init`、`go work use ./xxx` |
| `go bug` | 打开浏览器提交 bug 报告 | 会自动附带系统信息 |
## 交叉编译
### 基本用法
```bash
# Linux/macOS
GOOS=目标系统 GOARCH=目标架构 go build -o 输出文件名 源文件
# Windows (PowerShell)
$env:GOOS="目标系统"; $env:GOARCH="目标架构"; go build -o 输出文件名 源文件
# Windows (CMD)
set GOOS=目标系统 && set GOARCH=目标架构 && go build -o 输出文件名 源文件
```
查看所有支持的平台
```bash
go tool dist list
```
### 常用组合
| 目标平台 | GOOS | GOARCH |
| ------------ | ------- | ------ |
| Linux 64位 | linux | amd64 |
| Linux ARM64 | linux | arm64 |
| Windows 64位 | windows | amd64 |
| macOS Intel | darwin | amd64 |
| macOS M1/M2 | darwin | arm64 |
### 示例
```bash
# Linux/Mac (Bash)
# 编译 Linux 64位
GOOS=linux GOARCH=amd64 go build -o app ./app/api/main.go
# 编译 Windows 64位
GOOS=windows GOARCH=amd64 go build -o app.exe ./app/api/main.go
# 编译 macOS ARM64 (M1/M2)
GOOS=darwin GOARCH=arm64 go build -o app ./app/api/main.go
# Windows (PowerShell)
# 编译 Linux 64位
$env:GOOS="linux"; $env:GOARCH="amd64"; go build -o app ./app/api/main.go
# 编译 Windows 64位
$env:GOOS="windows"; $env:GOARCH="amd64"; go build -o app.exe ./app/api/main.go
# 编译 macOS ARM64 (M1/M2)
$env:GOOS="darwin"; $env:GOARCH="arm64"; go build -o app ./app/api/main.go
```