go-sqlmock is a mock library implementing the Go sql/driver interface to simulate database driver behavior in tests without requiring a real database connection.
The tool addresses the problem of testing database interactions in isolation by providing a mock SQL driver that intercepts database calls and allows you to define expected queries and their responses. It works by implementing the standard Go sql/driver interface, so it integrates directly with the database/sql package without requiring changes to your application code. The library supports concurrent access, multiple connections, Go context features, and named SQL parameters. It enforces strict expectation order matching by default and includes no third-party dependencies.
Developers should choose this tool when they want to test database logic in unit tests without spinning up a real database. It suits projects following test-driven development where you need to verify that your code issues the correct SQL queries and handles responses appropriately. The tool provides customizable query matching through a QueryMatcher interface, allowing you to match queries by exact equality, regular expression, or custom logic. For cases where you need to test with an actual database while keeping state isolated, the README mentions go-txdb as an alternative that wraps database operations in transactions.
The project is stable and complete, with the maintainer explicitly noting that few changes should be expected for this reason. The library supports concurrent operations and multiple simultaneous connections. It handles sophisticated argument matching for types like time.Time through an Argument interface when simple value comparison is insufficient.