{"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 08: sqlx, database/sql with the boilerplate removed.\n\ngithub.com/jmoiron/sqlx wraps database/sql (sqlx.DB embeds \\*sql.DB, so everything from lesson 02 still works) and adds:\n\n  - struct scanning via \\`db:\"column\"\\` tags:  Get (one row), Select (many rows), StructScan\n  - named parameters:  NamedExec / NamedQuery with :field placeholders bound from a struct or map\n  - sqlx.In:  expands a slice into  IN (?, ?, ?)  because database/sql cannot bind a slice\n  - Rebind:  rewrites ? placeholders for the target driver ($1, $2 for PostgreSQL)\n\nCompare with lesson 02: there we wrote Scan(\u0026t.ID, \u0026t.ProjectID, ...) and a for rows.Next() loop by hand, in the right column order, for every query. With sqlx, adding a column means adding a struct field.\n\nRules and gotchas:\n\n  - Column names must map to struct fields; an unmapped column is an error (\"missing destination name\"). Select only what the struct has, or add \\`db:\"-\"\\` / extra fields.\n  - Without tags sqlx lower-cases field names (AssigneeID -\u003e assigneeid), so always tag.\n  - Select loads ALL rows into memory. For huge results iterate with Queryx + StructScan.\n  - sqlx is still runtime-checked: a typo in SQL or a tag is found when the query runs.\n  - sqlx.In returns a query with ? marks; call db.Rebind(query) before running it so it works on PostgreSQL too. An EMPTY slice makes sqlx.In return an error, so guard it.\n\nIn industry: sqlx is a very common middle ground. You still write plain SQL (full control, easy to optimise) but skip repetitive scanning code. Teams that want compile-time checked queries move on to sqlc (lesson 09).\n\n## Types\n\n```go\ntype Task struct {\n\tID         int64   `db:\"id\"`\n\tProjectID  int64   `db:\"project_id\"`\n\tAssigneeID *int64  `db:\"assignee_id\"`\n\tTitle      string  `db:\"title\"`\n\tStatus     string  `db:\"status\"`\n\tPriority   int     `db:\"priority\"`\n\tDueDate    *string `db:\"due_date\"`\n}\n```\nTask uses \\`db\\` tags to map columns to fields. Nullable columns are pointers: nil = NULL.\n\n```go\nfunc (t Task) String() string\n```\n\n","path":"github.com/vidyesh95/Go-Tutorial/03-sql-sqlite/08sqlx","name":"main","synopsis":"Lesson 08: sqlx, database/sql with the boilerplate removed.","isRedistributable":true}
