{"modulePath":"github.com/vidyesh95/Go-Tutorial","version":"v0.0.0-20261123112932-610d9af314ca","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package main\n\nLesson 02: database/sql, Go's standard database API.\n\ndatabase/sql is a thin, driver-agnostic layer. You import a driver for its side effect (\\`\\_ \"modernc.org/sqlite\"\\` registers the name \"sqlite\"), then talk only to \\*sql.DB.\n\nKey facts:\n\n  - sql.Open does NOT connect. It validates arguments and returns a lazy handle. Call db.PingContext to prove the database is reachable (do it once at startup).\n  - \\*sql.DB is NOT a connection. It is a concurrency-safe POOL. Create one per database, share it everywhere, and close it only at program shutdown.\n  - ExecContext   -\u003e statements with no rows back (INSERT/UPDATE/DELETE), returns sql.Result.\n  - QueryContext  -\u003e many rows; you MUST rows.Close() (defer it) and check rows.Err() after the loop.\n  - QueryRowContext -\u003e exactly one row; errors surface at .Scan(); no row = sql.ErrNoRows.\n  - Always pass a context so queries are cancelled on timeout / client disconnect.\n  - NULL handling: scanning NULL into a plain string/int fails. Use sql.Null\\[T] (generic, Go 1.22+), sql.NullString, or a pointer (\\*string). Pick one style and stay consistent.\n  - RETURNING id (SQLite 3.35+, PostgreSQL) is the portable way to get a new id. res.LastInsertId() works in SQLite/MySQL but NOT in PostgreSQL drivers.\n\nConnection pool settings (defaults: unlimited open, 2 idle, no lifetime limit):\n\n  - SetMaxOpenConns   cap on total connections. Protects the database from your app. SQLite allows one writer at a time, so small numbers are fine here.\n  - SetMaxIdleConns   connections kept warm for reuse. Too low = constant reconnecting.\n  - SetConnMaxLifetime / SetConnMaxIdleTime  recycle connections so load balancers, failovers and server-side timeouts do not leave you holding dead ones.\n\nGotchas:\n\n  - Forgetting rows.Close() leaks a connection; the pool eventually runs dry and hangs.\n  - Ignoring rows.Err() hides errors that happen mid-iteration (network drop, cancelled ctx).\n  - Do not use Query for INSERT/UPDATE; use Exec.\n\nIn industry: services open one \\*sql.DB at startup, ping it, set pool limits from config (often MaxOpen around 10-25 per instance, because instances x MaxOpen must stay below the database's connection limit), and hide queries behind a repository / store type.\n\n## Types\n\n```go\ntype Task struct {\n\tID         int64\n\tProjectID  int64\n\tAssigneeID sql.Null[int64] // NULL = unassigned\n\tTitle      string\n\tStatus     string\n\tPriority   int\n\tDueDate    sql.NullString // NULL = no deadline (stored as ISO-8601 text)\n}\n```\nTask mirrors the tasks table. Nullable columns use sql.Null types so NULL is representable.\n\n```go\nfunc (t Task) String() string\n```\n\n","path":"github.com/vidyesh95/Go-Tutorial/03-sql-sqlite/02database-sql","name":"main","synopsis":"Lesson 02: database/sql, Go's standard database API.","isRedistributable":true}
