Scripts2026年9月12日·1 分钟阅读

go-sqlite3 — SQLite3 Driver for Go via cgo

A cgo-based SQLite3 driver that implements Go's standard database/sql interface, enabling Go applications to use SQLite with familiar query patterns.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
go-sqlite3
直接安装命令
npx -y tokrepo@latest install 26cef45b-aeaa-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

go-sqlite3 is a cgo-based SQLite3 driver for Go that conforms to the standard database/sql interface. It lets Go developers work with SQLite databases using the same query API they would use for PostgreSQL or MySQL, keeping SQLite as a lightweight embedded option for applications that do not need a separate server.

What go-sqlite3 Does

  • Implements the database/sql/driver interface for SQLite3
  • Supports prepared statements, transactions, and context-based cancellation
  • Allows registering custom SQL functions, aggregators, and collations in Go
  • Provides access to SQLite extensions and compile-time options
  • Handles concurrent reads with WAL mode and serialized threading

Architecture Overview

go-sqlite3 wraps the SQLite C library using cgo. When imported, it registers itself with Go's database/sql package under the driver name "sqlite3". All SQL operations pass through cgo to the embedded SQLite engine, which reads and writes directly to a local file. Connections are managed by the sql.DB connection pool, and the driver maps Go types to SQLite storage classes.

Self-Hosting & Configuration

  • Requires a C compiler (gcc or clang) on the build machine due to cgo
  • Set CGO_ENABLED=1 before building; cross-compilation needs a matching C cross-compiler
  • Pass SQLite pragma options via the DSN: "file:app.db?_journal_mode=WAL&_busy_timeout=5000"
  • Enable or disable SQLite extensions at compile time using build tags
  • Use the _loc parameter to control timezone handling for datetime values

Key Features

  • Full database/sql compatibility for idiomatic Go usage
  • Custom function registration for domain-specific SQL logic
  • Support for SQLite's backup API to copy databases while in use
  • FTS5 full-text search and JSON1 extension support via build tags
  • Connection hooks for tracing, profiling, and authorization callbacks

Comparison with Similar Tools

  • modernc.org/sqlite — pure Go, no cgo; go-sqlite3 is faster but requires a C compiler
  • crawshaw.io/sqlite — low-level SQLite bindings; go-sqlite3 integrates with database/sql
  • GORM + SQLite — ORM layer on top; go-sqlite3 is the underlying driver GORM uses
  • pgx (PostgreSQL) — server-based driver; go-sqlite3 is embedded and file-based
  • go-duckdb — analytical queries; go-sqlite3 is for transactional and general-purpose use

FAQ

Q: Why does go-sqlite3 require cgo? A: It links against the C implementation of SQLite for full compatibility and performance. If you cannot use cgo, consider modernc.org/sqlite as a pure-Go alternative.

Q: Is go-sqlite3 safe for concurrent use? A: Yes. With WAL journal mode enabled, multiple goroutines can read concurrently while one writes. The sql.DB pool manages connection reuse.

Q: Can I use go-sqlite3 in a Docker container? A: Yes. Include gcc and the SQLite development headers in your build stage. Alpine-based images need musl-dev and gcc installed.

Q: How do I enable full-text search? A: Build with the tag: go build -tags "fts5". This compiles SQLite with FTS5 support.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产