database/sql
to provide the usual methods you’d expect when working with databases in Go.
The
go-libsql
package uses CGO. You can use github.com/tursodatabase/libsql-client-go/libsql
instead, but it doesn’t support embedded replicas.- Retrieve database credentials
- Install Go libSQL
- Connect to a local or remote Turso database
- Execute a query using SQL
- Sync changes to local database (optional)
1
Retrieve database credentials
You will need an existing database to continue. If you don’t have one, create one.Get the database URL:Get the database authentication token:Assign credentials to the environment variables inside
.env
.You will want to store these as environment variables.
2
Install
First begin by adding libSQL to your project:
Local / Embedded Replicas
Local / Embedded Replicas
Remote only
Remote only
3
Connect
Now connect to your local or remote database using the libSQL connector:
Embedded Replicas
Embedded Replicas
Local only
Local only
Remote only
Remote only
4
Execute
You can execute a SQL query against your existing database. Create a function to query your database that accepts the pointer to Now inside
sql.DB
as an argument:func main()
call queryUsers
and pass in the pointer to sql.DB
:5
Sync (Embedded Replicas only)
When using embedded replicas you should call The connector can automatically sync your database at a regular interval when using the
Sync()
on the connector to sync your local database with the primary database.WithSyncInterval
option: