sqlc for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

sqlc for Go: Generating Type-Safe Code from SQL

\n\n

sqlc is a compiler that generates Go code based on your SQL queries. Instead of manually writing functions for database operations, you write pure SQL, and sqlc creates typed, safe, and performant functions for you. This is a fundamentally different approach compared to ORM: you retain full control over SQL while eliminating the routine of writing boilerplate code.

\n\n

Why is this needed? First, sqlc guarantees type-safety at compile time. If you change a table schema or query, sqlc regenerates the code, and the Go compiler immediately points out all places that need adjustments. Second, performance: the generated code does not use reflection or make unnecessary allocations, making it nearly as fast as manually writing queries through database/sql. Third, it simplifies code review: SQL queries are clearly visible in .sql files, not hidden inside ORM methods.

\n\n

sqlc is ideal for projects where performance and query control are important: high-load APIs, financial systems, analytical platforms. It also works well with migrations (e.g., golang-migrate) and does not tie you to a specific DBMS — PostgreSQL, MySQL, and SQLite are supported.

\n\n

Installation

\n\n

sqlc is not a library but a command-line utility. It can be installed in several ways:

\n\n
# Via go install (recommended)\ngo install github.com/sqlc-dev/sqlc/cmd/sqlc@latest\n\n# Via Homebrew (macOS/Linux)\nbrew install sqlc\n\n# Download binary from GitHub\n# https://github.com/sqlc-dev/sqlc/releases
\n\n

After installation, check the version:

\n\n
sqlc version
\n\n

Quick Start

\n\n

Let's create a minimal project that demonstrates how sqlc works.

\n\n

1. Project Structure

\n\n
mkdir sqlc-demo && cd sqlc-demo\ngo mod init sqlc-demo
\n\n

2. sqlc Configuration

\n\n

Create a sqlc.yaml file in the project root:

\n\n
version: "2"\nsql:\n  - engine: "postgresql"\n    queries: "queries.sql"\n    schema: "schema.sql"\n    gen:\n      go:\n        package: "db"\n        out: "db"\n        sql_package: "pgx/v5"
\n\n

3. Database Schema

\n\n

File schema.sql:

\n\n
CREATE TABLE authors (\n    id   BIGSERIAL PRIMARY KEY,\n    name text NOT NULL,\n    bio  text\n);
\n\n

4. SQL Queries

\n\n

File queries.sql:

\n\n
-- name: GetAuthor :one\nSELECT * FROM authors\nWHERE id = $1;\n\n-- name: ListAuthors :many\nSELECT * FROM authors\nORDER BY name;\n\n-- name: CreateAuthor :one\nINSERT INTO authors (name, bio)\nVALUES ($1, $2)\nRETURNING *;\n\n-- name: DeleteAuthor :exec\nDELETE FROM authors\nWHERE id = $1;
\n\n

5. Code Generation

\n\n
sqlc generate
\n\n

After execution, generated files will appear in the db/ folder. Now you can use them in your code:

\n\n
package main\n\nimport (\n    "context"\n    "fmt"\n    "log"\n\n    "github.com/jackc/pgx/v5/pgxpool"\n    "sqlc-demo/db"\n)\n\nfunc main() {\n    ctx := context.Background()\n    pool, err := pgxpool.New(ctx, "postgres://user:pass@localhost:5432/testdb")\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer pool.Close()\n\n    queries := db.New(pool)\n\n    // Create an author\n    author, err := queries.CreateAuthor(ctx, db.CreateAuthorParams{\n        Name: "Brian Kernighan",\n        Bio:  "Co-author of The C Programming Language",\n    })\n    if err != nil {\n        log.Fatal(err)\n    }\n    fmt.Printf("Created author: %+v\\n", author)\n\n    // Get author by ID\n    fetched, err :=                    

Recommendations