pgvector-go
pgvector examples for Go
Supports pgx, pg, and Bun

Getting Started
Follow the instructions for your database library:
pgx
Insert a vector
_, err := conn.Exec(ctx, "INSERT INTO items (factors) VALUES ($1::float4[])", []float32{1, 2, 3})
Get the nearest neighbors to a vector
rows, err := conn.Query(ctx, "SELECT id FROM items ORDER BY factors <-> $1::float4[]::vector LIMIT 5", []float32{1, 2, 3})
See a full example
pg
Add a vector column
type Item struct {
Factors [3]float32 `pg:"type:vector(3)"`
}
Insert a vector
item := Item{
Factors: [3]float32{1, 2, 3},
}
_, err := db.Model(&item).Insert()
Get the nearest neighbors to a vector
var items []Item
err := db.Model(&items).OrderExpr("factors <-> ?", [3]float32{1, 2, 3}).Limit(5).Select()
See a full example
Bun
Add a vector column
type Item struct {
Factors []float32 `bun:"type:vector(3)"`
}
Insert a vector
item := Item{
Factors: []float32{1, 2, 3},
}
_, err := db.NewInsert().Model(&item).Exec(ctx)
Get the nearest neighbors to a vector
var items []Item
err := db.NewSelect().Model(&items).OrderExpr("factors <-> ?", []float32{1, 2, 3}).Limit(5).Scan(ctx)
See a full example
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
To get started with development:
git clone https://github.com/ankane/pgvector-go.git
cd pgvector-go
go mod tidy
createdb pgvector_go_test
go test ./...