Last active
March 17, 2025 17:11
-
-
Save SCP002/a24afa9f8aa6902c4ecadf3ac6ca117f to your computer and use it in GitHub Desktop.
Golang: Shell script to build your project to each OS / Architecture specified.
This file contains hidden or 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
#!/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 | |
go env -u GOOS | |
go env -u GOARCH | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment