Skip to content

Instantly share code, notes, and snippets.

@theGeekPirate
Last active April 5, 2024 12:58
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save theGeekPirate/6e6655941c9485babd08 to your computer and use it in GitHub Desktop.
Save theGeekPirate/6e6655941c9485babd08 to your computer and use it in GitHub Desktop.
Builds a Go production (debug info stripped) binary for all supported platforms (excluding mobile)
#!/bin/sh
#From https://golang.org/doc/install/source#environment
platforms=(aix android darwin dragonfly freebsd illumos js linux netbsd openbsd plan9 solaris windows)
arches=(386 amd64 arm arm64 mips mipsle mips64 mips64le ppc64 ppc64le s390x wasm)
#.go file to build
test "$1" && target="$1"
if ! test "$target"; then
target="main.go"
fi
binaryName="$2"
for platform in "${platforms[@]}"; do
for arch in "${arches[@]}"; do
goos=${platform}
goarch=${arch}
output="$binaryName"
output="$(basename $target | sed 's/\.go//')"
[[ "windows" == "$goos" ]] && output="$output.exe"
destination="$(dirname $target)/builds/production/$goos/$goarch/$output"
echo -e "\e[00;33mGOOS=$goos GOARCH=$goarch go build -ldflags \"-w\" -o $destination $target\e[00m"
GOOS=$goos GOARCH=$goarch go build -ldflags "-w" -o "$destination" "$target"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment