bindings/go/go-bindings-db.mdx
Generate Go functions bindings for the Turso - SQLite-compatible embedded database written in Rust. You must use C API bridge implemented in Rust which expose ABI compatible functions to interact with the database.
The main goal is to translate C code to Go-friendly bindings which will have no additional logic, but more ergonomic API:
You must generate bindings for C interface defined in turso.h file:
<File path="../../sdk-kit/turso.h" />General rules for driver implementation you MUST follow and never go against these rules:
turso_statement_execute and turso_statement_step methods: every statement must be either "stepped" or "executed"
execute ignores all rowspackage turso
// define all package level errors here
// note, that OK, DONE, ROW, IO are statuses - so you don't need to create errors for them
var (
TursoBusyErr = errors.New("turso: database is busy") // provide short but concise error message
...
)
// define all necessary constants first
type TursoStatusCode int32
// note, that the only real statuses are OK, DONE, ROW, IO - everything else is errors
const (
TURSO_OK TursoStatusCode = 0
...
)
type TursoType int32
const (
TURSO_TYPE_INTEGER TursoType = 1
...
)
// define opaque pointers as-is and accept them as exact arguments (e.g. func turso_database_connect(self TursoDatabase) ... - DO NOT add extra indirection)
type TursoDatabase *turso_database_t
// define all public binding types
// the public binding types MUST have fields with native safe go types
type TursoConfig struct { ... }
// define all necessary private C structs
// private C structs MUST have fields with low level types (e.g. uintptr, numbers)
type turso_config_t struct { ... }
// then, define C extern methods
var (
// always use c_ structs here - never mix them with exported public types
c_turso_setup func(config turso_config_t) turso_status_code_t
...
)
// implement a function to register extern methods from loaded lib
// DO NOT load lib - as it will be done externally
func register_turso_db(handle uintptr) error {
purego.RegisterLibFunc(&c_turso_setup, handle, "turso_setup")
...
}
// Go wrappers over imported C bindings
// always use exported public types here - never mix them with c_ structs
func turso_setup(config TursoConig) error { ... }
c_ prefix for C extern functionsturso_.*) and have fields with native safe go types (string, slice, etc)c_turso_.*) and have fields with low-level C-compatible go types (uintptr, numbers, etc)turso_status_code_t and out error parameter with proper native golang error
turso_statement_row_value_bytes and turso_statement_row_value_text methods which will use c_turso_statement_row_value_bytes_ptr and c_turso_statement_row_value_bytes_count extern methods under the hood
Inspect following docstring from the official purego repository in order to understand how marshalling works:
<Link url="https://raw.githubusercontent.com/ebitengine/purego/refs/heads/main/func.go" selector="RegisterFunc#comment" /> <Link url="https://raw.githubusercontent.com/ebitengine/purego/refs/heads/main/func.go" selector="RegisterLibFunc#comment" /> <Link url="https://raw.githubusercontent.com/ebitengine/purego/refs/heads/main/syscall_sysv.go" selector="NewCallback#comment" /> </Code> </Output>