⏱️ Reading time: 6 min
The same pointer, passed the same way to the same C function from a Go program, can take 48 nanoseconds or 76 microseconds: a difference of up to 1,600x without changing the result. That’s what an analysis from The Consensus published on November 15, 2026 found. It also found that cgo’s pointer check, the bridge that lets Go call C code, can panic on code its own documentation explicitly allows.
📑 En este artículo
- What cgo Is and Where This Check Comes From
- The Mechanism Behind the 1,600x Difference
- The Pointer Check That Panics on Allowed Code
- What the Hacker News Discussion Looked Like
- cgo vs. purego vs. SWIG: Three Ways to Call C
- When to Avoid cgo, and When There’s No Real Alternative
- What This Means If You Maintain C Bindings in Go
- References
What cgo Is and Where This Check Comes From
cgo is the tool in Go’s official toolchain that lets you write C code inside a .go file and call it as if it were a function from any other package. When go build detects the line import "C", it invokes cmd/cgo, which splits out the C code, compiles it with an external compiler such as clang or gcc, and generates the Go and C files that bridge the two worlds. The official cgo documentation describes this mechanism in detail for anyone who wants to check it firsthand.
That bridge has one strict rule: C code cannot hold on to a pointer to Go memory after the call ends, because Go’s garbage collector can move or free that memory without C knowing. To enforce that rule, cgo inserts a pointer check before every call. According to The Consensus’s analysis, that check is inconsistent: exhaustive and expensive in some cases, lax in others.
The Mechanism Behind the 1,600x Difference
The root of the problem isn’t C or the hardware, it’s a limitation in Go’s own runtime: it only knows how to invoke C functions with one fixed shape, one that takes a single pointer argument and returns nothing. For that to work with a real C function, say one that adds two integers, cgo generates bridging code that lays the arguments out consecutively on the stack and references them through that single pointer, marked with the //go:cgo_unsafe_args directive, according to the same Consensus analysis.
flowchart TD
A["Go code with embedded C blocks"] --> B["go build detects import C"]
B --> C["cmd/cgo generates bridging Go and C files"]
C --> D["C compiler (clang or gcc)"]
D --> E["Linker"]
E --> F["Final binary"]
That extra step (serializing arguments, going through the pointer check, and building the call with the fixed shape the runtime requires) is what separates the fast path from the slow one. The Consensus report itself measures both extremes of the same call with an almost imperceptible variation in the source code: 48 nanoseconds versus 76 microseconds, according to the original analysis.
The Pointer Check That Panics on Allowed Code
The problem isn’t only about performance. The Consensus reports cases where cgo’s pointer check panics on code that cgo’s own documentation explicitly allows, turning a valid call into a runtime error. For anyone maintaining a package with C bindings, that means passing tests in development doesn’t guarantee the same call will survive in production if the pointer pattern it receives changes.
💡 Tip: before assuming a cgo call is slow because of the C function, profile it with pprof. The overhead might be in the pointer check, not in the code you’re calling.
What the Hacker News Discussion Looked Like
The finding generated enough interest to spark two independent Hacker News discussions about the same article: a first thread and, days later, a second thread. The fact that the same piece resurfaced on the front page twice is itself a signal: an identical call that varies 1,600x in speed touches a nerve for anyone who uses cgo daily and treats its internal mechanics as a black box.
cgo vs. purego vs. SWIG: Three Ways to Call C
cgo isn’t the only way to call C code from Go, and the alternatives sidestep the problem in different ways.
| Tool | How It Crosses to C | What It Avoids |
|---|---|---|
| cgo | Compiles the embedded C and generates bridging code on every build | Nothing: the program is still exposed to the pointer check and the overhead The Consensus documented |
| purego (ebitengine/purego) | Loads the dynamic library at runtime with dlopen/dlsym and calls the symbol directly | Going through cmd/cgo and the C compiler, so it doesn’t inherit its pointer check |
| SWIG | Generates compile-time bindings for C or C++ into several languages, Go included | The manual work of writing the cgo block, although the code it produces still goes through cgo to compile and link |
When to Avoid cgo, and When There’s No Real Alternative
cgo is still necessary when the C library is large, changes often, or depends on callbacks and complex nested structs, as is the case with OpenSSL or FFmpeg: reimplementing it in pure Go isn’t realistic for most teams. purego works well for a handful of simple symbols from a dynamic library, but it doesn’t replace a full binding with callbacks into Go. SWIG adds one more tool to the build pipeline and a steep learning curve for a small project, and the code it generates still ends up compiled by cgo anyway.
On top of that, any binary that uses cgo with CGO_ENABLED=1 needs a C compiler available for the target platform, which complicates cross-compilation. Avoiding cgo entirely simplifies that deployment, but it shifts the maintenance cost: someone has to keep the Go reimplementation in sync with every version of the original library.
What This Means If You Maintain C Bindings in Go
If you maintain a Go package that wraps a C library (a database driver, an image codec, a hardware SDK), The Consensus’s finding gives you three concrete things to check. First, profile the package’s most frequent cgo calls with pprof before assuming the cost sits on the C side: the gap between 48 nanoseconds and 76 microseconds can be hidden in how the call is written, not in the function it invokes. Second, check whether any call passes structs with nested pointers, the pattern most likely to trigger the pointer check in its most expensive or most inconsistent form. Third, if the binding only needs a handful of functions from a dynamic library and no complex callbacks, evaluate purego as a targeted replacement before taking on the full cost of cgo.
References
- The Consensus, “cgo’s pointer check is all over the place”: original analysis with the 48ns to 76µs measurements and the panic cases on allowed code.
- Official cmd/cgo documentation: language reference for cgo’s syntax and rules.
- Hacker News: first discussion thread about the finding.
- Hacker News: second discussion thread about the same article.
Featured image: Foto de Mathew Schwartz en Unsplash
How each article is made (sources, testing and editorial criteria): How we work.
Did it work for you? Got a different error? Say so below: questions get answered and help the next reader.
Leave a comment
0 Comments