Let's say that I have a package main
which imports two other packages pkg1
and pkg2
. pkg1
and pkg2
are not executable packages, they are library packages. Let's say that pkg1
and pkg2
are very large, are maintained on different repositories, and that I would like to compile them separately into two .a
binaries. This seems to be possible with
$ go build -i -o ./pkg1/pkg1.a ./pkg1/
When I want to compile my main
package, I would like to import those two .a
binaries and link them statically as to obtain a single executable file. How can we do that on Windows ? Something like this:
$ go build -link ./pkg1/pkg1.a ./pkg2/pkg2.a -o main.exe ./main/main.go
Note that I don't want to use the C API (by building a .dll
and using it with Cgo), but the Go API. Meaning that I want to benefit fully from the capabilities of Go (type safe, garbage collected, etc). I also want to do that on Windows, an OS which is not supported by -buildmode=shared
or Go plugins as of today. I don't care if it involves low level commands and black magic.
Now, I know that this is not exactly the "Go way". go build
handles the dependencies automatically for us and is very good at it. But I would like to know if it is possible and how to do it nevertheless, as it can be a business requirement when building large binaries, or when shipping compiled libraries to clients for IP reasons.
As a secondary question, If what I want to do is possible, then could the resulting executable file eventually contain multiple instances of the same compiled dependencies (therefore making the executable bigger than necessary)? In that case, is there a way to make sure that the resulting executable would contain exactly one instance of each compiled dependencies?