astra-db-go is a Go client for interacting with DataStax Astra DB and Hyper-Converged Database.
Update to Go 1.23 or higher. Download it here.
Install the client:
go get github.com/datastax/astra-db-go/v2
You need an Astra DB database or a Hyper-Converged Database (HCD) for the client to connect to.
For Astra DB Serverless:
For Hyper-Converged Database (HCD):
Package-level documentation is available on pkg.go.dev.
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/sort"
)
type MyDocument struct {
Name string `json:"name"`
Price int `json:"price"`
Description string `json:"$vectorize"`
}
func main() {
ctx := context.Background()
// Initialize the client
client := astra.NewClient()
db := client.Database("<endpoint>", options.API().SetToken("<token>"))
// Create a collection
coll, err := db.CreateCollection(ctx, "ExampleCollection")
if err != nil {
log.Fatal(err)
}
// Insert documents
docs := []MyDocument{
{Name: "Apple", Price: 10, Description: "..."},
{Name: "Peach", Price: 5, Description: "..."},
{Name: "Walnut", Price: 8, Description: "..."},
}
ids, err := coll.InsertMany(ctx, docs)
if err == nil {
fmt.Printf("Inserted documents with IDs: %v\n", ids)
}
// Find a document with vector search (vectorize)
var result struct {
MyDocument
astra.WithSimilarity
}
err = coll.FindOne(ctx, filter.F{},
options.CollectionFindOne().
SetSort(sort.Vectorize("<search query>")).
SetIncludeSimilarity(true),
).Decode(&result)
if err == nil {
fmt.Printf("Match: '%s' (similarity: %f)\n", result.Name, result.Similarity)
}
}