Monkey patching in Go unit tests with gomonkey, just disable inlining first
AI-generated summary of reporting by GitHub Opensource Projects. Read the original for the full story.
Why it matters
Go developers can now monkey-patch functions like time.Now in tests without refactoring to interfaces, but only if they remember to disable inlining.
gomonkey is a free, open-source testing tool for the Go programming language. It lets developers temporarily swap out a piece of a program, say, a clock function, with a fake version during tests, something normally impossible in compiled languages. The catch is that a compiler shortcut called inlining must be turned off, or the swap silently fails. It is useful for developers maintaining older, hard-to-test code.
The gomonkey library (v3) lets Go teams patch functions in unit tests without refactoring legacy code to interfaces, potentially saving hours per dependency untangled. The trade-off is real risk: patches silently fail unless tests compile with -gcflags=-l, and runtime code modification can panic if the target runs on another goroutine. It is a tactical tool for deadline-pressed teams, not an idiomatic replacement for dependency injection.
gomonkey v3 monkey-patches Go functions by overwriting the target's machine-code entry point with a jump instruction to a replacement, working on amd64 and arm64. Inlined functions have no entry point, so patches require //go:noinline or compiling tests with -gcflags=-l, or they silently no-op. It supports patching package-level functions like time.Now, methods, call-count and argument verification. Limitations: undefined behavior if targets execute concurrently, test-only use recommended.
Read the original
This summary exists to save you time, not to replace the source.
Key terms
Was this summary useful?
One click, anonymous. It tells us which summaries to re-run.
Related reading
Code-driven AI illustrations are more useful than images from dedicated image models
Code-driven AI illustrations let designers edit assets directly in code, saving time on iteration and ensuring consistency, which is a game-changer for teams that need rapid, reusable visual content.
A hierarchical note-taking app for building large personal knowledge bases
Trilium lets users build and navigate massive personal knowledge bases without duplicating content, saving time on organization and retrieval.
Desktop apps in Go with native UI or web frontend
Go developers can now ship cross-platform desktop and mobile apps in a single small binary without cgo or heavy runtimes, cutting build complexity and deployment size.
OpenBitFun: an agent workspace with four harness modes and phone remote control
OpenBitFun lets agents run on any machine and be controlled from a phone, eliminating the need to choose between local power and remote access, saving developers time switching contexts.
Intel One Mono: a monospaced font designed with low-vision developers
Developers with low vision or eye fatigue can read code more clearly, reducing errors and improving productivity.