explaingit

jmoiron/sqlx

17,617GoAudience · developerComplexity · 2/5Setup · easy

TLDR

sqlx is a Go library that adds convenience on top of the standard database/sql package, letting you scan query results directly into structs and use named parameters without switching to a full ORM.

Mindmap

mindmap
  root((sqlx))
    What it does
      Struct row scanning
      Named parameters
      Drop-in wrapper
    Helpers
      Get single row
      Select into slice
      NamedExec
    Tech Stack
      Go
      PostgreSQL
      MySQL SQLite
    Audience
      Go developers
      Backend engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Scan SQL query results directly into Go structs instead of manually mapping each column to a variable.

USE CASE 2

Use named parameters like :user_id in SQL queries with values supplied from a Go struct or map.

USE CASE 3

Gradually adopt sqlx in an existing Go codebase since it wraps database/sql types and existing code keeps working.

USE CASE 4

Replace boilerplate row-scanning with the single-line Get and Select helpers in a REST API backend.

Tech stack

GoPostgreSQLMySQLSQLite

Getting it running

Difficulty · easy Time to first run · 30min

Requires a running relational database (PostgreSQL, MySQL, or SQLite) and its corresponding Go driver.

In plain English

sqlx is a Go library that adds a friendlier layer on top of database/sql, the standard package Go programs use to talk to relational databases like PostgreSQL, MySQL, or SQLite. The standard library is solid but very low-level: you write raw SQL, get back a rows cursor, and have to scan each column into a Go variable by hand. sqlx keeps that whole approach intact while adding the convenience features most people end up wanting. The library wraps the familiar types (DB, Tx, Stmt, Conn) with sqlx versions that have the same underlying interfaces, so existing code keeps working and you can adopt sqlx gradually. On top of that it adds three big things. First, it can marshal database rows directly into Go structs, maps, or slices, including embedded structs, using a db struct tag to match columns to fields. Second, it supports named parameters such as :first_name in both queries and prepared statements, with values supplied from a struct or a map. Third, the Get and Select helpers turn a query plus a destination variable into a single line of code: Get fills one struct, Select fills a slice. There is also a BindDriver call that lets you register the bindvar style used by less common database drivers at runtime. You would reach for sqlx when you want to keep writing your own SQL (no ORM magic) but you are tired of the boilerplate around scanning rows into structs. It pairs well with PostgreSQL via lib/pq and other database/sql drivers shown in the examples. The codebase is written in Go, installed with go get, versioned with Go modules, and aims to stay compatible with the most recent two Go releases.

Copy-paste prompts

Prompt 1
I'm building a Go REST API with PostgreSQL. Show me how to use sqlx to fetch a list of users into a slice of structs with a Select call, and a single user with Get.
Prompt 2
How do I use sqlx named parameters with a Go struct to insert a new record into a PostgreSQL table?
Prompt 3
Refactor this Go database/sql code that manually scans rows into a struct to use sqlx instead: [paste your code here].
Prompt 4
Set up a sqlx DB connection pool for PostgreSQL in Go and show me how to run a transaction that inserts two related records atomically.
Prompt 5
How do I use sqlx with embedded structs to scan a JOIN query result into a Go struct that has a nested address field?
Open on GitHub → Explain another repo

← jmoiron on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.