【Golang】编译 - 交叉编译(Cross Compilation)

Posted by 西维蜀黍 on 2020-08-02, Last Modified on 2021-09-21

GOOS and GOARCH

$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/386
darwin/amd64
darwin/arm
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
illumos/amd64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/s390x
nacl/386
nacl/amd64p32
nacl/arm
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64
windows/arm

The following table shows the possible combinations of GOOS and GOARCH you can use:

GOOS - Target Operating System GOARCH - Target Platform
android arm
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64

Cross Compilation

Mac 下编译 Linux 和 Windows 64位可执行程序

$ GOOS=linux GOARCH=amd64 go build main.go
$ GOOS=windows GOARCH=amd64 go build main.go12

Linux 下编译 Mac 和 Windows 64位可执行程序

$ GOOS=darwin GOARCH=amd64 go build main.go
$ GOOS=windows GOARCH=amd64 go build main.go12

Windows 下编译 Mac 和 Linux 64位可执行程序

$ GOOS=darwin GOARCH=amd64 go build main.go

$ GOOS=linux GOARCH=amd64 go build main.go

Referecnce