Home > Backend Development > Golang > How to Build Multiple Go Package Binaries Simultaneously Without a Top-Level `cmd` Folder?

How to Build Multiple Go Package Binaries Simultaneously Without a Top-Level `cmd` Folder?

DDD
Release: 2024-11-02 22:49:29
Original
714 people have browsed it

How to Build Multiple Go Package Binaries Simultaneously Without a Top-Level `cmd` Folder?

Building Multiple Go Package Binaries Simultaneously

The question revolves around building multiple package binaries simultaneously, as the default advice of using a top-level cmd folder doesn't seem to work. The provided code example shows a particular folder structure that enables building specific binaries correctly.

To build all binaries in one step using the go build command, a variation of the following is recommended:

cd $GOPATH/someProject
for CMD in `ls src/cmd`; do
  go build ./src/cmd/"$CMD"
done
Copy after login

This command iterates through the packages in the src/cmd directory and builds each package individually. The resulting binaries will be stored in their respective package directories.

Alternatively, if you don't wish to install the binaries into $GOPATH/bin, a script can be employed. This is a common practice in open source projects, where build scripts handle multiple binary production.

The following example script can be used:

cd $GOPATH/someProject
for CMD in `ls cmd`; do
  go build ./cmd/$CMD
done
Copy after login

This script iterates through the packages in the cmd directory and runs go build on each. The result is a set of binaries stored in their respective cmd package directories.

For further reference, the following popular projects provide examples of build scripts:

  • Grafana: https://github.com/grafana/grafana/blob/master/build.go
  • Torus: https://github.com/coreos/torus/blob/master/Makefile
  • Caddy: https://github.com/mholt/caddy/blob/master/dist/automate.go

The above is the detailed content of How to Build Multiple Go Package Binaries Simultaneously Without a Top-Level `cmd` Folder?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template