mock

GoMock is a mocking framework for Go that generates type-safe mock implementations of interfaces, integrating directly with the standard testing package.

Library
Go
vv1.6.0
9,341stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
46/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture78
Code Quality68
Innovation55
Learning Curve80

GoMock is the mocking framework that popularized the interface-mock-generation pattern in Go. It ships two parts: the gomock runtime package (Controller, Call, Matcher) that test code imports directly to set expectations and assertions, and the mockgen CLI tool that reads an interface definition — either by parsing source (-source) or via reflection on a compiled package — and emits a matching mock implementation as generated Go code. Once mocks are generated, tests use gomock.NewController(t) and a fluent EXPECT().Method(args).Return(value) API to declare expected calls, argument matchers, and return values, with the controller validating that every expectation was satisfied.

As of June 2023 the project is explicitly archived by its maintainers, who point users to go.uber.org/mock as the maintained fork carrying the same API forward. It remains widely referenced in existing Go codebases and is documented here as the original, still-in-use implementation — new projects should evaluate the uber/mock fork for ongoing fixes.

What You Get

  • The gomock package — Controller, Call, and Matcher types for declaring and verifying expected method calls in tests
  • The mockgen CLI — generates mock implementations from Go interfaces in either source mode (-source) or reflect mode (package + symbol list)
  • A fluent expectation API — EXPECT().Method(matchers...).Return(values...) with call-count constraints (Times, MinTimes, MaxTimes, AnyTimes) and ordering (After)
  • Built-in argument matchers — Eq, Any, Nil, Not, plus custom matcher support via the Matcher interface
  • Automatic cleanup on Go 1.14+ — Controller self-registers via t.Cleanup so ctrl.Finish() doesn’t need to be called explicitly
  • Customizable failure output — WantFormatter and GotFormatterAdapter let a test rewrite how expected/actual values are printed on mismatch

Common Use Cases

  • Isolating a unit under test from its dependencies by mocking interfaces those dependencies implement
  • Generating a mock for a third-party or standard-library interface (e.g. database/sql/driver) via reflect mode without touching that package’s source
  • Verifying interaction order and call counts between collaborating components in table-driven tests
  • Stubbing return values and side effects (DoAndReturn) to exercise error-handling and edge-case branches
  • Wiring mockgen into go:generate directives so mocks regenerate automatically when interfaces change

Under The Hood

Architecture The module splits into two independently usable pieces sharing one Go module: gomock is the runtime consumed directly by test code (controller.go owns the Controller that tracks a callSet of expected calls keyed by receiver+method, call.go models a single expectation with its matchers and actions, callset.go indexes expected vs. exhausted calls for lookup), while mockgen is a separate main package that never imports gomock at runtime — it only emits code that imports it. mockgen/parse.go builds an interface model either by walking a source AST (go/ast, go/types) in source mode or via mockgen/reflect.go, which compiles and executes a tiny reflection program against the target package in reflect mode; both paths funnel into a shared mockgen/model representation that the generator renders as Go source. This clean separation means the generated mocks have a minimal runtime dependency surface (gomock only) even though generation itself pulls in the full go/types toolchain.

Tech Stack Pure Go, module github.com/golang/mock, requiring golang.org/x/mod for parsing go.mod in reflect mode and golang.org/x/tools/imports for formatting generated code. No external runtime dependencies beyond the standard library for the gomock package itself; mockgen additionally shells out to go build/go run internally when constructing its reflection program. Distributed as a CLI installed via go install .../mockgen@version and a library imported as github.com/golang/mock/gomock.

Code Quality Both packages carry substantial table-driven test suites alongside their implementation files (controller_test.go at nearly 1,000 lines, call_test.go, matchers_test.go, mockgen_test.go, parse_test.go), and CI (.github/workflows/test.yaml) runs go vet, a full build, a shell-scripted integration test (ci/test.sh, ci/check_panic_handling.sh), and go test ./... across two Go versions. Error handling favors explicit TestReporter.Fatalf/Errorf calls with descriptive messages pointing at the failing call site rather than swallowing errors, and the code is conventionally formatted with standard Go naming. No linter config or generics-aware type safety beyond what go vet/go build provide, and the project itself states it is no longer maintained, so quality here reflects a frozen snapshot rather than ongoing improvement.

What Makes It Unique GoMock’s reflect-mode code generation — compiling and executing a throwaway program against the target package to discover interface shapes via go/reflect, rather than only parsing source — let it mock interfaces it had no source access to (including standard-library and third-party interfaces) years before this became a common expectation for Go mocking tools. That said, the pattern is no longer unique to GoMock: its own successor fork carries the identical approach forward, and the project’s current value is chiefly as the long-standing reference implementation many existing codebases still depend on.

Used by 11 apps in this directory

Rust
65%
Apache 2.0

agentgateway

AI Development · Developer Tools

4,640

An open source AI-native proxy that secures, observes, and governs agent-to-LLM, agent-to-tool, and agent-to-agent communication through MCP, A2A, and unified LLM routing.

View details
87
Repo Health
82
Technical
71
Dependency
Built with
Rust65%
Go23%
Updated 3 days ago
Go
55%
Apache 2.0

Authgear

Authentication

2,014

Open-source, self-hostable authentication platform with passkeys, biometric login, SSO, MFA, and GraphQL admin API — a full Auth0/Clerk/Firebase alternative for SaaS and mobile apps.

View details
88
Repo Health
81
Technical
69
Dependency
Built with
Go55%
HTML25%
TypeScript17%
Updated 4 days ago
Go
90%
Apache 2.0

CasaOS

Hosting Control Panel · File Storage

37,163

Your simple, elegant personal cloud OS for home data and apps

View details
51
Repo Health
71
Technical
65
Dependency
Built with
Go90%
Updated 1 years ago
TypeScript
54%
Other

Focalboard

Productivity · Project Management · Collaboration

26,441

Self-hosted, open source project management with Kanban, table, gallery, and calendar views — a privacy-first alternative to Trello, Notion, and Asana.

View details
58
Repo Health
82
Technical
65
Dependency
Built with
TypeScript54%
Go40%
Updated 3 months ago
TypeScript
49%
AGPL 3.0

Grafana

Monitoring · Analytics

76,498

The open-source observability platform that unifies metrics, logs, and traces from any data source into dynamic, queryable dashboards.

View details
95
Repo Health
91
Technical
65
Dependency
Built with
TypeScript49%
Go45%
Updated today
TypeScript
51%
Other

Mattermost

Team Chat · Collaboration · Devops

38,940

Open core, self-hosted team collaboration with chat, AI agents, voice calling, and deep DevOps integrations — all under your control.

View details
96
Repo Health
87
Technical
65
Dependency
Built with
TypeScript51%
Go40%
Updated yesterday
Go
94%
Other

NetBird

Security

28,735

Replace your VPN with a zero-trust WireGuard overlay network that auto-connects devices, enforces SSO and posture checks, and deploys in under 5 minutes.

View details
92
Repo Health
82
Technical
66
Dependency
Built with
Go94%
Updated today
Go
44%
GPL 3.0

Netdata

Monitoring · Devops

80,357

Real-time per-second metrics, ML-powered anomaly detection, and zero-config observability for any infrastructure.

View details
93
Repo Health
86
Technical
71
Dependency
Built with
Go44%
C32%
Rust15%
Updated today
Go
74%
Other

Notifuse

Marketing

2,089

Open-source, self-hosted alternative to Mailchimp, Brevo, and Klaviyo — send newsletters and transactional emails without per-email pricing or vendor lock-in.

View details
83
Repo Health
80
Technical
66
Dependency
Built with
Go74%
TypeScript23%
Updated yesterday

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search