How to Build Go Executables for Multiple Operating Systems

Apr 7, 2023

Go makes it easy to build executables for various platforms / operating systems / processor architectures. Simply set the GOOS and GOARCH environment variables and run go build as usual:

GOOS="linux" GOARCH="amd64" go build -o yourProduct .

For another project, I created a bash script that builds binaries for a predefined list of binaries the entire script looks like this:


set -e

rm -rf build

# Define the target operating systems and architectures and extension


for target in "${TARGETS[@]}"; do
    # Split the target operating system and architecture into separate variables
    IFS='/' read -r os arch extension <<< "${target}"

    # Define the output directory

    # Create the output directory if it doesn't exist
    mkdir -p "${output_dir}"

    # Build the program for the target operating system and architecture
    GOOS="${os}" GOARCH="${arch}" go build -ldflags="-s -w" -trimpath -o "${output_dir}/${output_name}" .

    # Zip the executable file
    cd "${output_dir}"
    zip "" "${output_name}"
    mv "" "../${product_name}_${os}_${arch}.zip"

    cd "${start_dir}"

    # Print a message indicating that the build was successful
    echo "Build for ${os}/${arch} successful."

The platforms can be defined in the TARGETS variable. You can find all valid combinations in the Go “Installing Go from source” documentation. The script creates the builds for each platform in corresponding folders and then creates a ZIP with the executable. The resulting build folder looks like this:

── build
   ├─ linux_386
   │  └─ yourProduct
   ├─ linux_amd64
   │  └─ yourProduct
   ├─ linux_arm64
   │  └─ yourProduct
   ├─ darwin_amd64
   │  └─ yourProduct
   ├─ darwin_arm64
   │  └─ yourProduct
   ├─ windows_386
   │  └─ yourProduct.exe
   ├─ windows_amd64
   │  └─ yourProduct.exe

If you need something similar, you can get the script on GitHub: serve/ I published it under BSD 3-Clause License.


You might also like