cgo
The Go feature that lets Go code call C code, which links the program to C libraries on the build and target machines.
In more detail
Some standard library packages, such as parts of the network name lookup code, can use cgo when it is enabled. Setting CGO_ENABLED=0 turns it off so the build produces a static binary that runs in scratch or distroless images. If a dependency truly needs C, you need the C library in the runtime image too.
Example
CGO_ENABLED=0 go build -o app .Related terms
- static binary — A program file that carries all the code it needs inside it and loads no shared libraries from the machine it runs on.
- C library — The basic library that almost every program uses to talk to the operating system kernel, such as glibc on Debian Linux or musl on Alpine Linux.
- cross compilation — Building a program on one kind of machine for a different operating system or processor, such as a Linux binary on a Mac.
- shared library — Ready-made code that many programs load at run time instead of each carrying its own copy.
Taught in
- Building Static Binaries — Concurrent Systems Programming with Go
- Shipping in Scratch and Distroless Images — Concurrent Systems Programming with Go
- Containerizing and Testing the Final Build — Concurrent Systems Programming with Go