go交叉编译
交叉编译不支持 CGO 所以要禁用它,一般来说设置CGO_ENABLED=0
- GOOS:目标平台的操作系统(darwin、freebsd、linux、windows)
- GOARCH:目标平台的体系架构(386、amd64、arm)
Mac 环境变量设置
Linux
alias go_build_linux_amd64="CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build"
alias go_build_linux_arm="CGO_ENABLED=0 GOOS=linux GOARCH=arm go build"
Windows
alias go_build_windows="CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build"
Linux 环境变量设置
mac
alias go_build_mac="CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build"
windows
alias go_build_windows="CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build"
编译
编译到Linux平台:
go_build_linux_amd64 main.go
go_build_linux_arm main.go
编译到Windows平台:
go_build_windows main.go
编译到Mac平台:
go_build_mac main.go