When doing go build github.com/.../foo
from any directory, the compiled binary will be placed in the directory from which I executed the command. When doing go build /github.com/.../cmd/...
I expected all of the compiled binaries to appear in my current directory. But they don't.
How can I compile multiple/all binaries of that package into my current work dir?
As the issue 23616 states:
The fact that
go build
doesn't build is counter-intuitive
You will be able to get multiple binaries in your current directory when... the issue 14295 "cmd/go: go build should be able to write multiple executables" will be completed.
But that will be for Go 1.13 (not 1.12, so late 2019)
This is so close to working. I'm not sure why it doesn't.
We should fix it for Go 1.12.
Clearly-o
can't be used with multiple binary targets, but the implicit "write to the current directory" should work, and instead it does nothing.There is a bit of discussion on the proposed CL, as well s the comment that the CL as implemented breaks
go build -a std cmd
.
See CL 143139/ (CL = "Change List").
You can compile your binary using the project's file path (instead of the package name) to generate them in your current directory.
For example:
$ go build ~/Go/src/github.com/.../cmd
And the binaries will be in your current directory.
From go build -h
:
When compiling multiple packages or a single non-main package, build compiles the packages but discards the resulting object, serving only as a check that the packages can be built.
But you can use this one-liner to achieve the same result (only the binaries directly under cmd will be built): find $GOPATH/src/github.com/.../cmd -mindepth 1 -maxdepth 1 | xargs -n1 go build