Wednesday, January 17, 2024

GoLang + SQLite, without CGo

 Zombie Zen - zombiezen.com/go/sqlite reaches 1.0

CGo-less SQLite Go package, zombiezen.com/go/sqlite, has finally reached version 1.0

This package provides a low-level Go interface to SQLite 3. It is a fork of crawshaw.io/sqlite that uses modernc.org/sqlite, a CGo-free SQLite package. It aims to be a mostly drop-in replacement for crawshaw.io/sqlite.

This package deliberately does not provide a database/sql driver. See David Crawshaw's rationale for an in-depth explanation. If you want to use database/sql with SQLite without CGo, use modernc.org/sqlite directly.

pure Go, no C

  • Full SQLite functionality via modernc.org/sqlite, an automatically generated translation of the original C source code of SQLite into Go


Package sqlite is a cgo-free port of SQLite. Although you could see mattn's driver (github.com/mattn/go-sqlite3) in go.mod file, we import it for tests only.

SQLite is an in-process implementation of a self-contained, serverless, zero-configuration, transactional SQL database engine.




Most people use the mattn/go-sqlite3 package to interact with SQLite in Go. This package uses cgo and bundles the SQLite C amalgamation files with the Go source.

But Go developers often prefer not to use cgo (see for example cgo is not go). I mention this because there happens to be an underrated translation of SQLite's C source code to Go. Since it is a translation of C to Go, you don't need to use cgo to call into it. Some developers find this idea compelling. And this Go translation is an impressive work in and of itself.



Package sqlite is a cgo-free port of SQLite. Although you could see mattn's driver (github.com/mattn/go-sqlite3) in go.mod file, we import it for tests only.

SQLite is an in-process implementation of a self-contained, serverless, zero-configuration, transactional SQL database engine.

Go and SQLite: when database/sql chafes
 2018-04-02, David Crawshaw

The Go standard library includes database/sql, a generic SQL interface. It does a good job of doing exactly what it says it does, providing a generic interface to various SQL database servers. Sometimes that is what you want. Sometimes it is not.

Generic and simple usually means lowest-common-denominator. Fancy database features, or even relatively common but not widely used features, like nested transactions, are not well supported. And if your SQL database is conceptually different from the norm, it can get awkward.


Using SQLite from Go

  • pkg.go.dev/modernc.org/sqlite: I choose this package because it’s a cgo-free Go implementation of SQLite. Thus, installing it doesn’t require you to have a working C compiler, to start with. What’s interesting is this bit of text from the sqlite website:

    SQLite is a C-language library …

    Thus, modernc.org/sqlite is a Go implementation of SQLite.

ISC vs. MIT license


No comments: