Is it possible to compile golang code with dependencies? For an executable file was small.
As mentioned in "Linking golang statically" (Vincent Batts):
As long as the source being compiled is native go, the go compiler will statically link the executable.
Though when you need to use cgo, then the compiler has to use its external linker.
A pure go program will show like this:
$> go build ./code-pure.go
$> ldd ./code-pure
not a dynamic executable
$> file ./code-pure
./code-pure: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
This isn't so with cgo, unless you add an extra flag like:
go build --ldflags '-extldflags "-static"' ./code-cgo.go
# or, with gccgo
go build -compiler gccgo --gccgoflags "-static" ./code-cgo.go
Reminder, even with Go 1.5 (which uses go for compilation, and not gc
), gccgo will still be there.