build命令
目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| C:\MyApplication\Work\Projects\go\hexo_util> │ go.mod │ go.sum │ main.go │ ├─git-commit-push │ git-commit-push.go │ ├─init-time │ init-time.go │ └─update-time update-time update-time.go
|
构建主应用程序
假设 main.go
是主应用程序的入口:
1
| go build -o hexo_util.exe main.go
|
跨平台构建示例
构建适用于不同操作系统和架构的二进制文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $env:GOOS = "windows" $env:GOARCH = "amd64" $env:CGO_ENABLED = "0" go build -o hexo_util_windows_amd64.exe main.go
$env:GOOS = "linux" $env:GOARCH = "amd64" $env:CGO_ENABLED = "0" go build -o hexo_util_linux_amd64 main.go
$env:GOOS = "darwin" $env:GOARCH = "amd64" $env:CGO_ENABLED = "0" go build -o hexo_util_darwin_amd64 main.go
|
构建子模块
对于每个子目录,导航到该目录并运行 go build
命令:
1 2 3 4 5 6 7 8 9 10 11
| cd git-commit-push go build -o git-commit-push.exe git-commit-push.go
cd ..\init-time go build -o init-time.exe init-time.go
cd ..\update-time go build -o update-time.exe update-time.go
|