database/sql

Database
import "database/sql"

Provides a generic interface around SQL databases. Used with driver packages like lib/pq or go-sql-driver.

Example

package main

import (
    "database/sql"
    _ "github.com/lib/pq"
)

func main() {
    db, _ := sql.Open("postgres", "postgres://localhost/mydb")
    defer db.Close()
    db.QueryRow("SELECT 1")
}

Key Types & Functions

DBOpenRowsRowTxStmtQueryRowExec

About database/sql

The database/sql package (imported as database/sql) belongs to the Database category of Go packages. Provides a generic interface around SQL databases. Used with driver packages like lib/pq or go-sql-driver.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The database/sql package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using database/sql in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages