project-layout icon indicating copy to clipboard operation
project-layout copied to clipboard

Go build - where to put produced binaries ?

Open iangregsondev opened this issue 4 years ago • 5 comments
trafficstars

Hi,

Using the conventions here, so I have cmd/my_app , internal/my_app/... etc

When I do a "go build" - where should the binary be placed?

I was a little confused, I saw build and deployment - but where should the binaries go that are produced by running "go build"

maybe a directory called bin/my_app ?, it should NOT be under cmd/my_app - I am assuming.

A little confused.

Does anybody give some insight into what they are doing?

My idea would be this directory would be excluded from source control as it just stores the go binaries that are built.

I am using go modules, so it should be inside my project directory somewhere - but not sure where.

Any ideas?

Thanks in advance

iangregsondev avatar Mar 14 '21 10:03 iangregsondev

There is no standard. Binaries can be anywhere. Note in .gitignore they're stripped off git's scraper.

Although since /cmd is for your main project applications they should be in /cmd

tgbv avatar Mar 17 '21 06:03 tgbv

Thanks @tgbv ,

That makes sense, thanks.

Confused about one thing though, you state the binaries are excluded via gitignore.. I presume you mean ".exe" on Windows ?

What about Linux or mac ?

Or do you mean you exclude all files from cmd unless they end in .go ?

iangregsondev avatar Mar 28 '21 14:03 iangregsondev

I presume you mean ".exe" on Windows ? What about Linux or mac ?

I meant ".exe" Linux binaries have no standard extension (check https://askubuntu.com/a/174356). I don't know how Mac handles binaries

Or do you mean you exclude all files from cmd unless they end in .go ?

I did not mean that, but it's a tricky question.

It makes sense to have the compiled binaries in /cmd but not to commit them on git repo --- which is why ".exe" files are excluded in gitignore. If linux had a "executable file extension" standard, gitignore would contain it as well.

However depending on app structure some compiled binaries may retrieve their configuration from a local config file on the disk. Which is why I now tend to agree with your point: "exclude all files from cmd unless they end in .go"

tgbv avatar Mar 29 '21 05:03 tgbv

It's pretty common for the generated binaries to end up in <your_project_dir>/bin. And in your .gitignore file you'd have a line like this (to avoid committing the binaries to git): /bin/

kcq avatar Apr 28 '21 20:04 kcq

You should be using -o flag to opt a location for your executables, otherwise it'll perform default behavior which is vague mostly.

go build -o <directory to be saved> <package main file>

go build -o .\bin\golearn.exe .\learn.go

sabitkondakci avatar Dec 04 '22 20:12 sabitkondakci