0PricingLogin
Go Academy · Lesson

Opening a Connection

Connect to a database.

The database/sql Package

Gos standard database/sql package provides a generic, database-agnostic API. You pair it with a driver for your specific database, like PostgreSQL or MySQL.

  • One API, many databases
  • Built-in connection pooling
  • Safe parameterized queries

Importing a Driver

The driver registers itself via a blank import. The underscore means you import it only for its side effect, registration, not to call it directly.

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

All lessons in this course

  1. Opening a Connection
  2. Querying Rows
  3. Prepared Statements
  4. Transactions
← Back to Go Academy