I have a repository with a group of nested go packages organized as follows:
$GOPATH/src/
- mypackage/common/utils.go
- mypackage/app1/main.go
- mypackage/app2/main.go
...
It compiles to a handful of binaries. For releasing, I'm cross-compiling for a multitude of platforms/archs (I deploy repeatedly with a different GOOS
and GOARCH
). I'm trying to write the compilation results to a directory of my choice, but I'm fighting with the toolchain.
I can:
-
Combine
GOBIN
andgo install
when compiling for my own architecture (i.e. not cross compiling):# build + output all binaries in /somedir/bin # this works great when compiling for my local architecture. GOBIN=/somedir/bin go install mypackage/app1 mypackage/app2 mypackage/app3
But unfortunately,
GOBIN
conflicts with cross-compilation e.g:# Throws: "cannot install cross-compiled binaries when GOBIN is set" GOBIN=/somedir/bin GOARCH=amdm64 GOOS=darwin go install mypackage/app1 mypackage/app2
-
Use
go build
withGOOS=X
andGOARCH=Y
for each subpackage# cross compile one of the binaries cd /output/darwin-386-bin/ && \ GOOS=darwin GOARCH=386 go build mypackage/app1 # but if given multiple binaries, there is no output. (as documented) GOOS=darwin GOARCH=386 go build mypackage/app1 mypackage/app2
When
go build
is given multiple packages, it no longer emits binaries -- it just checks that the code compiles. To emit all the binaries, it seems I have to rungo build
once for each subpackage, so it takes longer, esp when building with-a
.Another possible issue with using
go build
like this, is that it's potentially mixing binaries and intermediary results across multiple architectures in the same workspace. But maybe that's just a matter of taste. Hopefully the toolchain keeps cached results separate for different architectures.
How is this handled in practice? Should I treat each subpackage as an individual package, despite their shared common code (would it then be safe to build them all in parallel)?
Related articles:
go build vs go install: https://groups.google.com/forum/#!topic/golang-nuts/s8Csz3-7EXA (Little info on cross-compilation, or multipackage setups)
cross compile with go 1.5.: https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5 (does not specifically address multipackage / common code compilation. Also, the statement about having to recompile the standard library into /usr/local/go/pkg seems to no longer hold in 1.9.1, as far as I can tell)
Go build multiple nested packages: Go build multiple/nested packages? . ( The accepted answer,
go build ./...
behaves the same as passing multiple packages in a single build command, and therefore outputs no binaries. The thread also does not cover cross compilation)