Created
April 27, 2022 08:56
-
-
Save eSargin/d55e0d7cb65feeafa88995f697cbdd79 to your computer and use it in GitHub Desktop.
Go Cross Platform Build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Before we can use the script, we have to make it executable with the chmod command: | |
#chmod +x ./go-executable-build.sh | |
#then we can use it ./go-executable-build.sh yourpackage | |
#!/usr/bin/env bash | |
package=$1 | |
if [[ -z "$package" ]]; then | |
echo "usage: $0 <package-name>" | |
exit 1 | |
fi | |
package_name=$package | |
#the full list of the platforms: https://golang.org/doc/install/source#environment | |
platforms=( | |
#"darwin/386" | |
#"darwin/amd64" | |
#"darwin/arm" | |
#"darwin/arm64" | |
"dragonfly/amd64" | |
"freebsd/386" | |
"freebsd/amd64" | |
"freebsd/arm" | |
"linux/386" | |
"linux/amd64" | |
"linux/arm" | |
"linux/arm64" | |
"netbsd/386" | |
"netbsd/amd64" | |
"netbsd/arm" | |
"openbsd/386" | |
"openbsd/amd64" | |
"openbsd/arm" | |
"plan9/386" | |
"plan9/amd64" | |
"solaris/amd64" | |
"windows/amd64" | |
"windows/386" ) | |
for platform in "${platforms[@]}" | |
do | |
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 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment