How to structure Go application to produce multiple binaries?

The following folder structure is used in many popular go projects such as helm,kubicorn etc.

goproject/
├── bin
├── cmd
├── pkg
└── Makefile

The cmd directory will contain the different binaries, separated in directories.

cmd/
├── bin1
│   └── main.go
├── bin2
│   └── main.go
└── bin3
    └── main.go

pkg directory will contain all your reusable packages. In your case the common code used by the different binaries. This directory can also be named internal , learn more about it here.

pkg
├── reusablepackage1
└── reusablepackage2

The bin directory is optional, It can be used to store the generated binaries. In case you are generating binaries to $GOBIN this can be omitted.

bin/
├── bin1
├── bin2
└── bin3

The Makefile can be used for writing helpful scripts, such as generating binaries running test suite etc. You can have commands such as
make bin1,make bin2 etc.This is optional but I highly recommend this. because it saves me from writing long build commands over and over again.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)