project-layout
project-layout copied to clipboard
Go build - where to put produced binaries ?
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
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
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 ?
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"
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/
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