lab/note/资源/编程/Golang/常用命令.md
张育新 be711334fb docs(note): 添加笔记目录及 gitignore 规则
- 新增 note 目录包含 AI、SQL、Golang、网络等多领域学习笔记
- 更新 .gitignore 添加 note/assets/ 和 note/项目/ 忽略规则
2026-07-10 09:24:40 +08:00

56 lines
1.3 KiB
Markdown

## 交叉编译
### 基本用法
```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
```