Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.turso.tech/llms.txt

Use this file to discover all available pages before exploring further.

1

Install

Add the Turso package to your Go project:
go get turso.tech/database/tursogo
2

Connect

Here’s how you can connect to a local SQLite database:
package main

import (
    "database/sql"
    "fmt"
    _ "turso.tech/database/tursogo"
)

func main() {
    db, err := sql.Open("turso", "sqlite.db")
    if err != nil {
        panic(err)
    }
    defer db.Close()
}
3

Create table

Create a table for users:
_, err = db.Exec(`
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT NOT NULL
  )
`)
if err != nil {
    panic(err)
}
4

Insert data

Insert some data into the users table:
_, err = db.Exec("INSERT INTO users (username) VALUES (?)", "alice")
if err != nil {
    panic(err)
}

_, err = db.Exec("INSERT INTO users (username) VALUES (?)", "bob")
if err != nil {
    panic(err)
}
5

Query data

Query all users from the table:
rows, err := db.Query("SELECT * FROM users")
if err != nil {
    panic(err)
}
defer rows.Close()

for rows.Next() {
    var id int
    var username string
    if err := rows.Scan(&id, &username); err != nil {
        panic(err)
    }
    fmt.Printf("User: ID: %d, Username: %s\n", id, username)
}