Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Last active August 16, 2019 16:15
Show Gist options
  • Save HirbodBehnam/5d9b5b416f08263239e9b9f417188d32 to your computer and use it in GitHub Desktop.
Save HirbodBehnam/5d9b5b416f08263239e9b9f417188d32 to your computer and use it in GitHub Desktop.
Cross compile golang on linux with this script.
#!/usr/bin/env bash
#Main file from https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04
#I just edited the platform; I'm just backing up it here
package=$1
if [[ -z "$package" ]]; then
echo "usage: $0 <package-name>"
exit 1
fi
package_split=(${package//\// })
package_name=${package_split[-1]}
platforms=("windows/amd64" "windows/386" "darwin/amd64" "darwin/386" "linux/386" "linux/amd64" "linux/arm" "linux/arm64")
mkdir build
for platform in "${platforms[@]}"
do
echo "Building for $platform"
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$package_name'-'$GOOS'-'$GOARCH
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
mv $output_name build/
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment