Golang supports the cross-compilation function of generating executable programs for another platform under one platform
go build
, Is a very commonly used command, it can start compilation and compile our package and related dependencies into an executable file
//查看环境变量
go env
*note*
Two important environment variables GOOS and GOARCH
GOOS refers to the target operating system:
darwin
freebsd
linux
windows
android
dragonfly
netbsd
openbsd
plan9
solaris
GOARCH refers to the architecture of the target processor:
arm
arm64
386
amd64
ppc64
ppc64le
mips64
mips64le
s390x
The effective combination of GOOS and GOARCH supports the generation of multiple executable programs https://golang.org/doc/install/source#environment
The valid combinations of $GOOS and $GOARCH are:
$GOOS $GOARCH
aix ppc64
android 386
android amd64
android arm
android arm64
darwin amd64
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
illumos amd64
js wasm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
linux s390x
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
openbsd arm64
plan9 386
plan9 amd64
plan9 arm
solaris amd64
windows 386
windows amd64
Compile 64-bit executable programs for Linux and Windows platforms under Mac:
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go
Compile 64-bit executable programs for Mac and Windows platforms under Linux:
$ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go
Compile 64-bit executable programs for Mac and Linux platforms under Windows:
$ SET CGO_ENABLED=0 SET GOOS=darwin3 SET GOARCH=amd64 go build main.go
$ SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build main.go