We are compiling our Go code to be run on docker and we were investigating why our binary was not executing. We found that it was missing some dynamic libraries (although we want statically linked binaries).
This is how it was compiled.
env GOOS=linux CGO_ENABLED=1 GO111MODULE=on GOPRIVATE=github.com/ourrepo GOPROXY=https://proxy.golang.org go build --installsuffix cgo --ldflags='-extldflags=-static' -o program main.go
Using the same build command with CGO_ENABLED=0 ended up fixing the issue, and the output binary was statically linked.
Now the weird part is we have another program that is using the same build command, this time with CGO_ENABLED=1 and... it is statically linked!
So I'm very confused why in some cases CGO_ENABLED=1 produces dynamic linking, and sometimes static linking. Happy to provide more details.