sqlx: Struct Scanning and Named Queries
Get, Select, NamedExec with sqlx
sqlx: Struct Scanning and Named Queries is a free Go Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
What is sqlx?
github.com/jmoiron/sqlx extends database/sql with struct scanning, named queries, and in-query binding. It is API-compatible with database/sql.
sqlx.Open
Open a database connection using sqlx instead of sql:
db, err := sqlx.Open("postgres", dsn)
if err != nil { log.Fatal(err) }Get and Select
db.Get scans a single row into a struct; db.Select scans multiple rows into a slice:
var user User
db.GetContext(ctx, &user, "SELECT * FROM users WHERE id=$1", id)
var users []User
db.SelectContext(ctx, &users, "SELECT * FROM users WHERE active=$1", true)Struct tags
sqlx maps columns to struct fields using the db tag:
type User struct {
ID int `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
}Named queries
Use named parameters with :name syntax and pass a struct or map:
query := "INSERT INTO users (name, email) VALUES (:name, :email)"
_, err := db.NamedExecContext(ctx, query, user)sqlx.In
sqlx.In expands a slice into a SQL IN clause:
ids := []int{1, 2, 3}
query, args, _ := sqlx.In("SELECT * FROM users WHERE id IN (?)", ids)
query = db.Rebind(query)
db.SelectContext(ctx, &users, query, args...)Rebind
db.Rebind(query) converts ? placeholders (used by sqlx.In) to the driver-specific format ($1, $2 for PostgreSQL):
StructScan
Scan an individual row into a struct using rows.StructScan:
rows, _ := db.QueryxContext(ctx, "SELECT * FROM users")
for rows.Next() {
var u User
rows.StructScan(&u)
}MustExec
db.MustExec executes a query and panics on error — useful for schema migrations or test setup where errors are always fatal.
db.MustExec(schema)Unsafe
db.Unsafe() returns a version that silently ignores unmapped columns instead of returning an error — useful when the SELECT returns more columns than the struct has fields.
sqlx vs ORM
sqlx gives you full SQL control with Go-native struct binding. ORMs like GORM hide SQL but can generate unexpected queries. Use sqlx when you want explicit SQL with ergonomic scanning.
Quick Check
What does db.SelectContext do in sqlx?
Recap: sqlx
Key points:
- Get/Select scan directly into structs using db tags
- Named queries with :name syntax and NamedExecContext
- sqlx.In + Rebind for IN clause with slices
- StructScan for per-row struct scanning in loops
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “sqlx: Struct Scanning and Named Queries” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “sqlx: Struct Scanning and Named Queries”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “sqlx: Struct Scanning and Named Queries”?
Get, Select, NamedExec with sqlx You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “sqlx: Struct Scanning and Named Queries” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- database/sql and Drivers
- sqlx: Struct Scanning and Named Queries
- pgx: High-Performance PostgreSQL Driver
- Transactions and Migrations