database/sql

Go's standard interface to SQL databases. A driver such as go-sql-driver/mysql registers itself with a blank import, and you work through *sql.DB with QueryContext, QueryRowContext and ExecContext, always passing a context.

Minimal example

go
db, err := sql.Open("mysql", os.Getenv("MYSQL_DSN"))
row := db.QueryRowContext(ctx, "SELECT name FROM restaurants WHERE id = ?", id)

Learn this concept

Lesson 2.4 · database/sql: pools, contexts and timeouts

Used in exercises

Exercise 2.6 · Generate the restaurant and menu queries Exercise 2.7 · Seed 50 restaurants and one full menu