If you try to compile the following Go program:
package main
import _ "crypto/tls"
func main() {
}
You'll wind up with a dynamically linked Go binary. This is annoying for me (I'm building a Go binary inside a Docker container, which uses a different libc from my host, which will result in the binary not running on my host).
How does one force Go to build such a program statically?
GoLang by default produces statically linked binaries. RustLang by default produces dynamically linked binaries.
Go creates static binaries by default unless you use cgo to call C code, in which case it will create a dynamically linked binary.
Go does not currently support dynamic linking. Well, it can link C . dll/. so files okay, but there's no way to dynamically link to a Go binary (or create a .
Statically-Linked means that all the code you use from Go's standard library, as well as third-party packages, is compiled into your built executable. When combined with the fully-compiled quality, this means that distributing a Go program to another machine is literally as simple as copying a single file.
The only OS where crypto/tls
uses cgo is darwin, where it needs to call FetchPEMRoots
to get the root CAs.
The reason your program is using cgo is because crypto/tls
imports the net
package, which links to the host resolver by default. You can build the net
package without cgo using the "netgo" build tag.
go build -tags netgo
Or if you're on a release where the std lib packages won't be built by default, you can trigger them to be compiled with a new installsuffix
go build -installsuffix netgo -tags netgo
Since you're not going to have the need or ability to use cgo in your environment, you may want to just build everything with CGO_ENABLED=0
to disable cgo entirely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With