Connect to SQLite in Go by importing a driver, registering it, and opening a connection with sql.Open.
Connect to SQLite by importing a driver, registering it with database/sql, and opening a connection using the file: URI scheme.
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:mydb.sqlite3?cache=shared")
if err != nil {
panic(err)
}
defer db.Close()
}
Connecting to SQLite from Go links your application to a local database file so you can run SQL queries. Think of it as plugging a USB drive into your computer; the driver is the port, and the connection is the drive itself. You use this whenever your app needs to store or retrieve structured data without a separate server.