Go
# Go SDK
The official Go module. Context aware, no dependencies outside the standard library.
The Go module takes a `context.Context` on every call, so timeouts and cancellation work the way you expect. It only depends on the standard library.
## Install
```bash
go get zeroindex.test/go
```
## Set up the client
```go
package main
import (
"os"
zeroindex "zeroindex.test/go"
)
func main() {
client := zeroindex.New(os.Getenv("ZEROINDEX_KEY"),
zeroindex.WithHost("https://api.zeroindex.test"),
)
_ = client
}
```
## Add documents and search
```go
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
ctx := context.Background()
products := client.Index("products")
task, err := products.AddDocuments(ctx, []Product{
{ID: 1, Name: "Wireless headphones", Price: 129},
{ID: 2, Name: "Desk lamp", Price: 59},
})
if err != nil {
log.Fatal(err)
}
client.WaitForTask(ctx, task.TaskUID)
var hits []Product
_, err = products.Search(ctx, "wirless", &zeroindex.SearchParams{Limit: 5}, &hits)
```
## Errors
Errors from the API are a `*zeroindex.Error` with the `Code` from the [error table](/api/errors).
```go
var apiErr *zeroindex.Error
if errors.As(err, &apiErr) && apiErr.Code == "index_not_found" {
// Create the index, then retry.
}
```
<Callout type="tip">
The client is safe to share between goroutines. Create it once at startup.
</Callout>
The official Go module. Context aware, no dependencies outside the standard library.
zeroindex.test/gov1.6.0Go 1.22 or laterThe Go module takes a context.Context on every call, so timeouts and cancellation work the way you expect. It only depends on the standard library.
Install
go get zeroindex.test/goSet up the client
package main
import (
"os"
zeroindex "zeroindex.test/go"
)
func main() {
client := zeroindex.New(os.Getenv("ZEROINDEX_KEY"),
zeroindex.WithHost("https://api.zeroindex.test"),
)
_ = client
}Add documents and search
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
ctx := context.Background()
products := client.Index("products")
task, err := products.AddDocuments(ctx, []Product{
{ID: 1, Name: "Wireless headphones", Price: 129},
{ID: 2, Name: "Desk lamp", Price: 59},
})
if err != nil {
log.Fatal(err)
}
client.WaitForTask(ctx, task.TaskUID)
var hits []Product
_, err = products.Search(ctx, "wirless", &zeroindex.SearchParams{Limit: 5}, &hits)Errors
Errors from the API are a *zeroindex.Error with the Code from the error table.
var apiErr *zeroindex.Error
if errors.As(err, &apiErr) && apiErr.Code == "index_not_found" {
// Create the index, then retry.
}