sqlc for Go: Generating Type-Safe Code from SQL
\n\nsqlc 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\nWhy 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.
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\nInstallation
\n\nsqlc 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\nAfter installation, check the version:
\n\nsqlc version\n\nQuick Start
\n\nLet's create a minimal project that demonstrates how sqlc works.
\n\n1. Project Structure
\n\nmkdir sqlc-demo && cd sqlc-demo\ngo mod init sqlc-demo\n\n2. sqlc Configuration
\n\nCreate a sqlc.yaml file in the project root:
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\n3. Database Schema
\n\nFile schema.sql:
CREATE TABLE authors (\n id BIGSERIAL PRIMARY KEY,\n name text NOT NULL,\n bio text\n);\n\n4. SQL Queries
\n\nFile queries.sql:
-- 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\n5. Code Generation
\n\nsqlc generate\n\nAfter execution, generated files will appear in the db/ folder. Now you can use them in your code:
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 := Also in library
PHP QR Code Generator: phpqrcode library and alternatives
Working with Apache Kafka in C++: librdkafka Library and Practical Examples
Email Sending in JavaScript (Node.js) — Libraries and Code Examples
HTTP Client for Go (Golang): Top Libraries for Network Operations
tsx for TypeScript: Complete Guide with Examples