Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Go application not statically linked?

Tags:

go

I had expected a static binary when using Go.

ubuntu@ugbuildserver:~/gospace$ go build src/runk/runk.go
ubuntu@ugbuildserver:~/gospace$ file runk
runk: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
ubuntu@ugbuildserver:~/gospace$

Any suggestions for what is wrong?

like image 561
Duke Dougal Avatar asked Sep 06 '25 16:09

Duke Dougal


1 Answers

Go still dynamically links to some shared library functions when using packages like net and os/user.

Go 1.5 further reduced these requirements for the net package: https://golang.org/doc/go1.5#net

The DNS resolver in the net package has almost always used cgo to access the system interface. A change in Go 1.5 means that on most Unix systems DNS resolution will no longer require cgo, which simplifies execution on those platforms. Now, if the system's networking configuration permits, the native Go resolver will suffice. The important effect of this change is that each DNS resolution occupies a goroutine rather than a thread, so a program with multiple outstanding DNS requests will consume fewer operating system resources.

Advanced reading: http://dominik.honnef.co/posts/2015/06/statically_compiled_go_programs__always__even_with_cgo__using_musl/

like image 79
elithrar Avatar answered Sep 09 '25 17:09

elithrar