Home >Backend Development >Golang >How to Build Multiple Go Binaries Simultaneously?
Building Multiple Package Binaries Simultaneously
As per your inquiry, building multiple package binaries simultaneously can be achieved by creating a top-level "cmd" folder structure, which has been suggested elsewhere. However, you have encountered challenges with this approach.
To build individual binaries using a "cmd" folder structure:
Ensure that you have the following file structure (as per your working example):
├── cmd │ ├── bin1 │ │ └── main.go │ ├── bin2 │ │ └── main.go ├── src │ ├── shared │ │ ├── foo │ │ │ └── foo.go
Execute the following command for each binary you wish to build, replacing "
go build -o "<binary-name>" ./cmd/<binary-name>
For example:
go build -o bin1 ./cmd/bin1 go build -o bin2 ./cmd/bin2
This will create the "bin1" and "bin2" binaries in the project's root directory.
Alternative Approach Using a Script
If you prefer not to install the binaries into $GOPATH/bin, you can create a build script that iterates over the packages and builds each binary individually.
Create a script named "build-all.sh" with the following contents:
<code class="sh">#!/bin/bash # Iterate over the packages in the "cmd" directory for CMD in `ls cmd`; do # Build the binary for each package go build -o $CMD ./cmd/$CMD done</code>
Make the script executable:
<code class="sh">chmod +x build-all.sh</code>
Run the script to build all binaries in one step:
<code class="sh">./build-all.sh</code>
This approach provides flexibility and control over the binary build process and mimics what many open source projects use.
The above is the detailed content of How to Build Multiple Go Binaries Simultaneously?. For more information, please follow other related articles on the PHP Chinese website!