Last active
March 8, 2026 21:28
-
-
Save jpmcb/0408583f31bf6663c2a2f79f68cdcff3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "fmt" | |
| "log" | |
| _ "github.com/mattn/go-sqlite3" | |
| _ "turso.tech/database/tursogo" | |
| ) | |
| // This program uses both tursogo and mattn/go-sqlite3 compiled via CGO together. | |
| // Since tursogo dynamically bundles and loads its sqlite libs via go:embed, | |
| // CGO never sees those symbols so both can co-exist. | |
| func main() { | |
| // Turso DB | |
| tursoConn, err := sql.Open("turso", "turso.db") | |
| if err != nil { | |
| log.Fatal("turso open:", err) | |
| } | |
| defer tursoConn.Close() | |
| _, err = tursoConn.Exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT)") | |
| if err != nil { | |
| log.Fatal("turso create table:", err) | |
| } | |
| _, err = tursoConn.Exec("INSERT INTO users (username) VALUES (?)", "alice_turso") | |
| if err != nil { | |
| log.Fatal("turso insert:", err) | |
| } | |
| fmt.Println("Wrote to Turso DB") | |
| // SQLite3 DB | |
| sqliteConn, err := sql.Open("sqlite3", "sqlite3.db") | |
| if err != nil { | |
| log.Fatal("sqlite3 open:", err) | |
| } | |
| defer sqliteConn.Close() | |
| _, err = sqliteConn.Exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT)") | |
| if err != nil { | |
| log.Fatal("sqlite3 create table:", err) | |
| } | |
| _, err = sqliteConn.Exec("INSERT INTO users (username) VALUES (?)", "bob_sqlite3") | |
| if err != nil { | |
| log.Fatal("sqlite3 insert:", err) | |
| } | |
| fmt.Println("Wrote to SQLite3 DB") | |
| fmt.Println("\n--- Turso DB users ---") | |
| readUsers(tursoConn) | |
| fmt.Println("\n--- SQLite3 DB users ---") | |
| readUsers(sqliteConn) | |
| } | |
| func readUsers(db *sql.DB) { | |
| stmt, err := db.Prepare("SELECT id, username FROM users") | |
| if err != nil { | |
| log.Fatal("prepare:", err) | |
| } | |
| defer stmt.Close() | |
| rows, err := stmt.Query() | |
| if err != nil { | |
| log.Fatal("query:", err) | |
| } | |
| defer rows.Close() | |
| for rows.Next() { | |
| var id int | |
| var username string | |
| if err := rows.Scan(&id, &username); err != nil { | |
| log.Fatal("scan:", err) | |
| } | |
| fmt.Printf("User: ID: %d, Username: %s\n", id, username) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ CGO_ENABLED=1 go build -o test . | |
| $ ./test | |
| Wrote to Turso DB | |
| Wrote to SQLite3 DB | |
| --- Turso DB users --- | |
| User: ID: 1, Username: alice_turso | |
| --- SQLite3 DB users --- | |
| User: ID: 1, Username: bob_sqlite3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment