Cross Compile
Build command:
$ GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
go tool dist list
Get $GOOS
and $GOARCH
list:
$ 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
Build multiple bins
$ vi build_go_bins.sh
#!/usr/bin/env bash
package=$1
if [[ -z "$package" ]]; then
echo "usage: $0 <package-name>"
exit 1
fi
package_split=(${package//\// })
package_split_len=${#package_split[@]}
if [ "$package_split_len" -eq "1" ]; then
package_split_2=(${package//./ })
package_split_2_len=${#package_split_2[@]}
if [ "$package_split_2_len" -eq "1" ]; then
package_name=$package
else
package_name=${package_split_2[0]}
fi
else
package_name=${package_split[-1]}
fi
platforms=("windows/amd64" "windows/386" "darwin/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$package_name'-'$GOOS'-'$GOARCH
echo "Compiling $output_name ..."
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
done
Get source file: build_go_bins.sh.txt
$ wget https://go.cloudbook.wiki/build_go_bins.sh.txt -O build_go_bins.sh
$ chmod +x build_go_bins.sh
$ ./build_go_bins.sh helloworld.go
Tools
xgo - Go CGO cross compiler
h
Although Go strives to be a cross platform language, cross compilation from one platform to another is not as simple as it could be, as you need the Go sources bootstrapped to each platform and architecture.
Reference
h
h