Announcing the release of the Xata Go SDK
Tags: go, database, developer, sdk
Published: 2023-12-07 07:00:00

Earlier this year we shared a [community spotlight](https://xata.io/blog/community-spotlight-xata-go-sdk) focused on a Go SDK developed by a dedicated contributor, [xata-go](https://github.com/kerdokurs/xata-go). 

[Kerdo Kurs](https://www.linkedin.com/in/kerdokurs/), the developer behind this project, was a huge inspiration for us committing to an official Go SDK. Today, we're thrilled to announce the official release of our Xata Go SDK, and open the doors to the growing Go community 🎉

The `v0.0.1` release features our most popular endpoints, enabling functionalities like searching a branch, using the ask endpoint for follow-up questions, and creating a database, among others. You can see the evolving endpoint coverage in the ticket [xata-go#1](https://github.com/xataio/xata-go/issues/1).

If you want to get started right away, head over to the Go SDK [documentation](https://xata.io/docs/sdk/go/overview) and browse the code on [GitHub](https://github.com/xataio/xata-go).

In our documentation, we've included examples showing how to use the Go SDK, similar to the existing guides we have for TypeScript, Python, cURL, and SQL.

## Anatomy of the SDK

In the Go SDK, components are organized into specialized clients within distinct namespaces, like the `RecordsClient` for record-related functions.

All endpoints that are listed in our [API reference](https://xata.io/docs/api-reference/db/db_branch_name/transaction), are modeled within this client.

The available clients are:

- `BranchClient`
- `DatabasesClient`
- `FilesClient`
- `RecordsClient`
- `SearchAndFilterClient`
- `TableClient`
- `UsersClient`
- `WorkspaceClient`

The code and corresponding test suites can be found in the [xata package](https://github.com/xataio/xata-go/tree/main/xata).

If you're familiar with our SDKs, you'll notice familiar API naming patterns, adapted to align with Go's idiomatic style.
To illustrate the usage of the Go SDK, we curated a couple of examples. Browse [our docs](https://xata.io/docs/sdk/get) to see the full bandwidth of examples.

The following example succinctly demonstrates, how to use the Ask endpoint.

For demo purposes we assume that the table `IMDB` contains all movies listed from [IMDB.com](https://www.imdb.com/) and we want to learn how many Ace Ventura movies exist.

More complex queries and follow up questions examples can be found [here](https://xata.io/docs/sdk/ask).

```go
searchClient, _ := xata.NewSearchAndFilterClient()
result, _ := searchClient.Ask(context.TODO(), xata.AskRequest{
  TableName: "IMDB",
  Question:  "How many Ace Ventura movies are there?",
})
```

In this example, we have not normalized data in multiple tables in one branch and want to search across this entire branch.

We are looking for the name _Philip_, but as there are multiple ways of writing Philip (such as _Filip_ or _Phillip_ or _Philippe_) we need to apply fuzziness to our search. `Fuzziness: xata.Int(2)` sets the search fuzziness level to a degree of 2, allowing for slight variations in the spelling of search terms.

```go
searchClient, _ := xata.NewSearchAndFilterClient()
results, _ := searchClient.SearchBranch(context.TODO(), xata.SearchBranchRequest{
  Payload: xata.SearchBranchRequestPayload{
    Query:     "Philip",
    Fuzziness: xata.Int(2),
  },
})
```

The last example illustrates the use of the [transaction](https://xata.io/docs/sdk/transaction) endpoint.
Transactions are a powerful way to do multiple operations in one go.

For the sake of the example, we solely want to create multiple records of actors that participated in the movie the Matrix.

```go
recordsClient, _ := xata.NewRecordsClient()
result, _ := recordsClient.Transaction(context.TODO(), xata.TransactionRequest{
  Operations: []xata.TransactionOperation{
    xata.NewInsertTransaction(xata.TransactionInsertOp{
      Table: "Actors",
      Record: map[string]any{
        "name": xata.String("Keanu Charles Reeves"),
        "movie": xata.String("the_matrix"),
      },
      Record: map[string]any{
        "name": xata.String("Carrie-Anne Moss"),
        "movie": xata.String("the_matrix"),
      },
      Record: map[string]any{
        "name": xata.String("Laurence Fishburne"),
        "movie": xata.String("the_matrix"),
      },
    }),
  },
})
```

## What’s next?

The SDK is an alpha release, and not all [endpoints are covered](https://github.com/xataio/xata-go/issues/1), we continue to increase the API coverage of the SDK and provide bug fixes.

We can't do it without you! If you want to contribute [code](https://github.com/xataio/xata-go), add an [example](https://github.com/xataio/xata-go/tree/main/examples) update the [docs](https://xata.io/docs/sdk/go/overview), or found a bug please open a PR or issue and we will assist you. All contributions are welcome ✅

To stay up to date with that latest at Xata, follow us on [X | Twitter](https://twitter.com/xata) or pop in and say "hi" 👋 in [Discord](https://xata.io/discord).

Is there a language you wished we supported natively but don't today? Feel free to open up a [feature request](https://xata.canny.io/feature-requests) or check-in with our amazing community.