Skip to content

Instantly share code, notes, and snippets.

@SCP002
Created July 28, 2024 14:49
Show Gist options
  • Save SCP002/a24afa9f8aa6902c4ecadf3ac6ca117f to your computer and use it in GitHub Desktop.
Save SCP002/a24afa9f8aa6902c4ecadf3ac6ca117f to your computer and use it in GitHub Desktop.
Golang: Shell script to build your project to each OS / Architecture specified.
#!/bin/bash
# Tested with go 1.22.5
project_name="my_project" # Change to your project name
main_file="${project_name}.go" # Or main.go, depends on your project structure
build_path="./build"
# Add values to your liking from "go tool dist list" command
# or https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
os_list=(
"darwin"
"freebsd"
"linux"
"netbsd"
"openbsd"
"windows"
)
arch_list=(
"386"
"amd64"
"arm"
"arm64"
"loong64"
"mips"
"mips64"
"mips64le"
"mipsle"
"s390"
"s390x"
)
for os in "${os_list[@]}"; do
if [[ $os == "windows" ]]; then
extension=".exe"
else
extension=""
fi
for arch in "${arch_list[@]}"; do
go env -w GOOS=$os 2> /dev/null
go env -w GOARCH=$arch 2> /dev/null
if [[ $? -eq 0 ]]; then
echo Building for $os / $arch
go build -o "${build_path}/${project_name}_${os}_${arch}${extension}" $main_file
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment