API Reference
go-sqlmock
A mock library for Go database/sql that lets tests simulate SQL queries without a real database.
Official Documentation ↗CompatibilityThis documentation covers version
v1.5. Check the official docs for the latest API surface.Installation
go
go get github.com/DATA-DOG/go-sqlmockMethods
No methods match this filter.
go-sqlmock returns a database handle whose expectations are declared with regex matchers.
Key Features
- Regex or exact query matching
- Argument and result expectations
- Ordered and unordered expectations
Usage Notes
Pin a version and check the official docs for the API surface you depend on before upgrading.
Errors
The official API reference declares the following error types:
sqlmock.ErrCancelled(var error (errors.New)) — official text: “var ErrCancelled = errors.New(“canceling query due to user request”)” (source: https://raw.githubusercontent.com/DATA-DOG/go-sqlmock/v1.5.2/sqlmock_go18.go, fetched 2026-11-08T16:02:45+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:02:45+08:00 via WebFetch pkg.go.dev + PowerShell Invoke-WebRequest raw.githubusercontent.com (DATA-DOG/go-sqlmock v1.5.2) + 模块 zip (proxy.golang.org v1.5.2).
sqlmock.New→ signature from https://raw.githubusercontent.com/DATA-DOG/go-sqlmock/v1.5.2/driver.go; official text: “func New(options …func(*sqlmock) error) (*sql.DB, Sqlmock, error) {”mock.ExpectQuery→ signature from https://raw.githubusercontent.com/DATA-DOG/go-sqlmock/v1.5.2/sqlmock.go; official text: “// ExpectQuery expects Query() or QueryRow() to be called with expectedSQL query. // the *ExpectedQuery allows to mock database response ExpectQuery(expectedSQL string) *ExpectedQuery func (c *sqlmock) ExpectQuery(expect”mock.ExpectExec→ signature from https://raw.githubusercontent.com/DATA-DOG/go-sqlmock/v1.5.2/sqlmock.go; official text: “func (c *sqlmock) ExpectExec(expectedSQL string) *ExpectedExec {”
FAQ
- How do I assert expectations were met?
- Call mock.ExpectationsWereMet() in teardown to fail when a declared expectation was never called.
- Why does my query not match?
- The matcher is a regex by default. Escape special characters or use regexp.QuoteMeta for literal queries.
- Can I test transactions?
- Yes. ExpectBegin, ExpectCommit, and ExpectRollback declare transaction lifecycle expectations.
Report a problem with this API docOpens a pre-filled issue on GitHub.