Xata SDK for Go
The Go SDK is currently in alpha. This means that it does not include all endpoints and is subject to potential
breaking changes before reaching its 1.0.0
stable General Availability (GA) release.
The Go SDK is available on GitHub: https://github.com/xataio/xata-go
go get github.com/xataio/xata-go@latest
The client requires the XATA_API_KEY
environment variable for authentication. You can configure your workspace, region, and branch in two ways:
The following environment variables are available to configure your connection params:
XATA_WORKSPACE_ID
: This is variable to mandatory.XATA_BRANCH
: This is optional, if unspecified, defaults tomain
.XATA_REGION
: This is optional, if unspecified, defaults tous-east-1
. Explore all available regions in Xata.
Alternatively, you can configure settings using the .xatarc
file. To initialize your project with the .xatarc
file, refer to the installation guide.
After setting up your project and adding Xata credentials, create an example Go file.
The following example code demonstrates using the SDK to connect to Xata, retrieve a list of workspaces, and then print out details of the first workspace in the list.
In your text editor, paste the following code into your example.go
file and save the file:
package main
import (
"context"
"fmt"
"github.com/xataio/xata-go/xata"
)
func main() {
workspaces, _ := xata.NewWorkspacesClient()
resp, _ := workspaces.List(context.TODO())
fmt.Printf("%#v\n", *resp.Workspaces[0])
}
In the terminal, enter go run .
to run the code.
The code snippet calls workspaces.List(ctx)
API to list all workspaces and prints the details of the first workspace with fmt.Printf("%#v\n", *resp.Workspaces[0])
.
You can run the snippet from above on your machine by, pulling the example code from the xataio/xata-go repository
wget -O example.go "https://raw.githubusercontent.com/xataio/xata-go/main/examples/list_workspaces.go"
go mod edit -module=example.com/mod
go mod download github.com/xataio/xata-go@main
Run the code with the command:
XATA_API_KEY="<INSERT_YOUR_API_KEY>" go run example.go
- Numeric operations are currently not supported using the update API. As a workaround, you can use the transaction API example.
- Not all Xata endpoints are available at the alpha state of the SDK. You can check the coverage here.
- Currently, there is no option to change the consistency and the default setting is
strong
.