Esc
ZeroIndex
GuidesAPI referenceSDKsChangelogOverviewBuy ZeroIndex

Go

View as MarkdownThe page as plain text

The official Go module. Context aware, no dependencies outside the standard library.

zeroindex.test/gov1.6.0Go 1.22 or later

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

go get zeroindex.test/go

Set 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
}
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.
}