# Define the list of target platforms# To list all Go platform: "go tool dist list"PLATFORMS="
darwin/amd64
darwin/arm64
linux/amd64
windows/amd64
"# Check go.mod and go.sumif [ ! -f go.mod ] || [ ! -f go.sum ]; then echo "$PWD is not a Go project." echo "$(basename "$0") must be run at root of a Go project" exit 1fiAPP=$(basename "$PWD")DIR_BUILD=build
# Create the dist directory if it doesn't existfor PLATFORM in $PLATFORMS; do# Extract the GOOS and GOARCH values from the platform string export GOOS=$(echo "$PLATFORM" | cut -d/ -f1) export GOARCH=$(echo "$PLATFORM" | cut -d/ -f2)DIR_PLATFORM=$DIR_BUILD/$GOOS/$GOARCH mkdir -p "$DIR_PLATFORM"# Determine the binary extension based on the target OSBIN_EXT=""if [ "$GOOS" = "windows" ]; thenBIN_EXT=".exe"fi# Build the executable echo "Building $APP for $GOOS/$GOARCH..." go build -o "$DIR_PLATFORM/$APP$BIN_EXT"done
Modify PLATFORMS to add or remove platforms for building. Get platform list by go tool dist list.