I have noticed that when using go build
the binary result can be in excess of 2MB; but using gccgo
the binary is less than 35k.
The other issue that I noticed when using gccgo
is that the produced binary isn't runnable on another linux box (missing libgo.so
I believe) but the go build
binary runs just fine (so I imagine its because the binary includes all that is necessary to run?); is there a way to do this with gccgo
?
You have to use the -static flag:
Use the -static option to do a fully static link (the default for the gc compiler).
You can link libgo statically, while still linking the system libraries dynamically, by using the -static-libgo option. (This applies to gccgo only).
With go 1.8 or above, if go is built with CGO_ENABLED=1
(default to 1), a native compilation links dynamically. Check this variable by running go env CGO_ENABLED
. You can switch to link statically by set the environment variable CGO_ENABLED to 0 when running go build.
CGO_ENABLED=0 go build
BTW, cross compiling uses static linkage automatically.