
sqlx
Go helper library that adds convenience features on top of the standard database/sql package.
- Stars
- 17.7k
- Forks
- 1.1k
- Licence
- MIT
- Language
- Go
- Last update
- 2.2 years ago
- Type
- Library
No commits in over a year — check for forks or alternatives before adopting.
What it is
sqlx is a Go library that extends the standard database/sql package. Its database types keep the same interfaces as the originals, so existing code can be adopted gradually. It can map query rows into structs, maps, and slices, supports named parameters, and offers Get and Select helpers to load a single result or a list directly.
- Load a list of database rows into a Go slice of structs with a single Select call
- Fetch one row into a struct with Get
- Run inserts that use named parameters taken from a struct or a map
- Insert many records in one batch from a slice
Great for
- Go developers who already use database/sql and want less boilerplate when reading rows
- Projects that want named query parameters such as :first_name
- Code that maps query results onto Go structs
- Teams that need batch inserts from slices of structs or maps
Not ideal for
- Developers who want a full ORM with models, migrations, and relationship handling, since the README describes only query and mapping extensions
- Projects not written in Go
- Complex queries where column names are ambiguous, because the README warns that struct or map mapping can be unclear in those cases
How to get it
- 1
Install with a package manager
You'll need: Go (the README says it supports the two most recent Go versions; exact version not stated) · A database driver package, such as github.com/lib/pq for Postgres, as shown in the README example
Install the library
go get github.com/jmoiron/sqlx - 2
Run it
- Install the library with go get github.com/jmoiron/sqlx
- Import sqlx along with a database driver such as github.com/lib/pq
- Connect with sqlx.Connect, passing the driver name and connection string
- Define a struct with db tags and use db.Select or db.Get to read rows into it
- 3
Get the most out of it
- Use named queries such as :first_name with structs or maps to avoid manual argument ordering
- Use batch insertion by passing a slice of structs or maps to NamedExec
- Use BindDriver to register bindvar types for drivers sqlx does not know about, per the README
- Use AS aliases in queries to avoid ambiguous column names
- Read sqlx_test.go in the repository for more usage examples
Downloads
Strengths
- Keeps the standard database/sql interfaces, so it fits into existing codebases
- Reduces repetitive row-scanning code through struct and slice mapping
- Has been around long enough to have wide adoption, with about 17.7k GitHub stars
Trade-offs
- The README notes that duplicate or ambiguous column names can make struct and map mapping unclear, so queries need explicit AS aliases
- Behavior of multi-statement Exec differs between database drivers, according to the README
- Compatibility is promised only for the two most recent Go versions
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| sqlx (this) | 17.7k | Library | intermediate | 2.2 years ago | Go developers who already use database/sql and want less boilerplate when reading rows |
| gorm | 40k | Library | intermediate | 13 days ago | Go developers building web services or APIs that store data in a relational database |
| sqlc | 18.4k | Dev tool | intermediate | 4 days ago | Go developers who prefer writing plain SQL over using an ORM |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.