Skip to content

Instantly share code, notes, and snippets.

@jlainezs
Created October 4, 2023 09:46
Show Gist options
  • Save jlainezs/bedf01af918283fb723a50073e2b6d27 to your computer and use it in GitHub Desktop.
Save jlainezs/bedf01af918283fb723a50073e2b6d27 to your computer and use it in GitHub Desktop.
Compile golang source for linux and windows
#!/usr/bin/env bash
# Build tool
# Writes the compiled executables in ./build
#
# see https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04
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" "linux/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name='./build/'$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
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment