go build -o output_directory ./...

On Tuesday, June 30, 2020 at 12:58:05 PM UTC+2 b.ca...@pobox.com wrote:

> Suppose I have a tree of files like this, to build two executables:
>
> ==> ./go.mod <==
> module example.com/test
>
> go 1.14
>
> ==> ./shared.go <==
> package example
>
> const Answer = 42
>
> ==> ./bin/test1/main1.go <==
> package main
>
> import (
> . "example.com/test"
> "fmt"
> )
>
> func main() {
> fmt.Printf("%s, answer is %d\n", Message, Answer)
> }
>
> ==> ./bin/test1/dep1.go <==
> package main
>
> const Message = "hello"
>
> ==> ./bin/test2/main2.go <==
> package main
>
> import (
> . "example.com/test"
> "fmt"
> )
>
> func main() {
> fmt.Printf("%s, answer was %d\n", Message, Answer)
> }
>
> ==> ./bin/test2/dep2.go <==
> package main
>
> const Message = "goodbye"
>
> Ideally I'd like a way to build the executables in a single command.  
> Currently I enter each directory and build explicitly, i.e.
>
> for d in bin/*; do (cd $d && go build); done
>
> If at the top level I do "go build ./...", the executables don't get 
> built.  Something happens, because it takes a fraction of a second to 
> return, but nothing is written out as far as I can see.
>
> The documentation <https://golang.org/cmd/go/> says:
>
> -buildmode=default
>       Listed main packages are built into executables and listed
>       non-main packages are built into .a files (the default
>       behavior).
>
>
> This implies multiple "main packages" can be built into multiple 
> "executables" in one command.
>
> But how is a main package "listed"?  I tried:
>
> $ go build bin/test1
> can't load package: package bin/test1 is not in GOROOT 
> (/usr/local/go/src/bin/test1)
> $ go build bin/test1/main
> can't load package: package bin/test1/main is not in GOROOT 
> (/usr/local/go/src/bin/test1/main)
> $ go build bin/test1/...
> go: warning: "bin/test1/..." matched no packages
> go: warning: "bin/test1/..." matched no packages
> $ go build bin/test1/main1.go
> # command-line-arguments
> bin/test1/main1.go:9:35: undefined: Message
>
> The following is accepted, but as far as I can see does not create any 
> files:
>
> $ go build example.com/test/bin/test1 example.com/test/bin/test2
> $ 
>
> Just like "go build ./...", it takes a noticeable fraction of a second, 
> but nothing is written out.  I've look in the top level, inside the 
> bin/test[12] directories, and  "find ~/go -mtime -1"
>
> If I build one at a time, it works, yay!
>
> $ go build example.com/test/bin/test1
> $ go build example.com/test/bin/test2
> $ ls
> bin  go.mod  shared.go test1  test2
>
> ... but the documentation says I ought to be able to list multiple main 
> packages, so I'm clearly missing something.
>
> It seems when I build a single package, it gets moved to the final 
> location:
>
> $ go build -x example.com/test/bin/test1
> ...
> /usr/local/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/exe/a.out # 
> internal
> mv $WORK/b001/exe/a.out test1
> rm -r $WORK/b001/
> $ 
>
> but not when I build two:
>
> $ go build -x example.com/test/bin/test1 example.com/test/bin/test2
> ...
> /usr/local/go/pkg/tool/linux_amd64/buildid -w $WORK/b033/exe/a.out # 
> internal
> /usr/local/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/exe/a.out # 
> internal
> $ 
>
> That's as far as I've got.  (Incidentally, I see the same with "go build 
> -x ./...")
>
> Any clues?
>
> Thanks,
>
> Brian.
>
> P.S.
> $ go version
> go version go1.14.4 linux/amd64
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e0ca3474-2f02-4e8d-bc36-b5af043b7139n%40googlegroups.com.

Reply via email to