I am trying to create statically linked test binary for code which uses net
package. But it gives me dynamically linked test binary. My code:
package main
import "net"
func main() {
net.ParseIP("1.1.1.1")
}
Test:
package main
import "testing"
func TestMain(t *testing.T) {
main()
}
I run command:
CGO_ENABLED=0 go test -c -installsuffix netgo -a -o ./static && file ./static && ldd ./static
it gives:
./static: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
linux-vdso.so.1 => (0x00007fffa1491000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3ed433c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3ed3f77000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3ed455a000)
If create non-test binary it works as I expect:
CGO_ENABLED=0 go build -installsuffix netgo -a -o ./static && file ./static && ldd ./static
./static: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
not a dynamic executable
If I remove net
package dependency or use go 1.6
, I can compile statically linked test binary. But I need do it with go 1.4.2 and net
package dependency.
Also, I tried more flags:
go test -c -installsuffix netgo,cgo -tags netgo,cgo -ldflags '-s' -a -o ./static && file ./static && ldd ./static
But it gives me dynamically linked binary anyway.