Go SDK (Zero cgo)

Pure Go SPD2 + TLS driver, context timeouts, Pool, prepared statements, transactions, and exact Value readers.

1 min read Official SDKs (12 Languages)

The official Go driver (github.com/spyddb/spyddb-go) is pure Go with zero cgo dependencies. It speaks SpydWire v2 (SPD2) binary framing over TCP or TLS 1.2/1.3, supports context.Context cancellation/deadlines, typed Value inspection (u64 above MaxInt64, exact Decimal, Timestamp, NULL), prepared statements, transactions, and concurrency-safe Pool reuse.

1. Installation#

sh
go get github.com/spyddb/spyddb-go/spyddb

Requires Go 1.21+.

2. Opening a TLS Client, Querying & Exact Value Accessors#

go
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    "github.com/spyddb/spyddb-go/spyddb"
)

func main() {
    dsn := fmt.Sprintf(
        "spyddb://app:%s@127.0.0.1:5433/default?tls=true&ca=docker/tls/ca.crt&server_name=localhost",
        os.Getenv("SPYD_TOKEN"),
    )

    client, err := spyddb.Open(dsn)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    if err := client.Ping(ctx); err != nil {
        log.Fatal(err)
    }

    _, err = client.Exec(ctx,
        "create collection if not exists shipments { id: u64 primary, tracking: text, weight: f64, verified: bool, created_at: timestamp }")
    if err != nil {
        log.Fatal(err)
    }

    // Parameterized insert preserving huge u64 and typed params
    affected, err := client.Exec(ctx,
        "shipments | insert [{ id: $1, tracking: $2, weight: $3, verified: $4, created_at: $5 }]",
        uint64(18446744073709551000),
        "SPYD-9901",
        4.25,
        true,
        time.Now().UTC(),
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Inserted:", affected)

    rows, err := client.Query(ctx, "shipments | where verified == $1 | pick id, tracking, weight, created_at", true)
    if err != nil {
        log.Fatal(err)
    }

    for _, row := range rows {
        // Use AsUint64() for U64 above MaxInt64; AsInt64() safely returns (_, false) on overflow
        id, ok := row.ValueOf("id").AsUint64()
        tracking, _ := row.ValueOf("tracking").AsString()
        weight, _ := row.Value(2).AsFloat64()
        ts, _ := row.Value(3).AsTime()
        fmt.Printf("ok=%v id=%d tracking=%s weight=%.2f ts=%s\n", ok, id, tracking, weight, ts.Format(time.RFC3339))
    }
}

3. Prepared Statements, Database Switching & Transactions#

go
// Switch active database on this session
if _, err := client.UseDatabase(ctx, "default"); err != nil {
    log.Fatal(err)
}

// 1. Prepare once, execute repeatedly, close deterministically
stmt, err := client.Prepare(ctx, "shipments | where tracking == $1 | pick id, weight")
if err != nil {
    log.Fatal(err)
}
defer client.ClosePrepared(ctx, stmt)

matches, err := client.QueryPrepared(ctx, stmt, "SPYD-9901")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Prepared rows:", len(matches))

// 2. Interactive SSI transaction
if err := client.Begin(ctx); err != nil {
    log.Fatal(err)
}
if _, err := client.Exec(ctx, "shipments | insert [{ id: $1, tracking: $2, weight: 2.0, verified: false, created_at: now() }]", uint64(2), "SPYD-9902"); err != nil {
    _ = client.Rollback(ctx)
    log.Fatal(err)
}
if err := client.Commit(ctx); err != nil {
    log.Fatal(err)
}

4. Concurrency-Safe Connection Pooling (Pool)#

Client serializes work on a single socket; use Pool across HTTP handlers and worker goroutines:

go
pool, err := spyddb.NewPool(dsn, 32, 8) // maxOpen=32, maxIdle=8
if err != nil {
    log.Fatal(err)
}
pool.MaxLifetime = 15 * time.Minute
defer pool.Close()

rows, err := pool.Query(ctx, "shipments | agg count() as total")
if err != nil {
    log.Fatal(err)
}
if len(rows) == 1 {
    if count, ok := rows[0].Value(0).AsUint64(); ok {
        fmt.Println("Total shipments:", count)
    }
}

For manual pool checkout (for example around multi-statement transactions):

go
conn, err := pool.Acquire(ctx)
if err != nil {
    return err
}
defer pool.Release(conn) // call pool.Discard(conn) instead if network I/O broke the socket

5. Exact Value Accessor Reference#

  • v.Null() bool — strictly distinguishes NULL from empty strings
  • v.AsUint64() (uint64, bool) — safe for full 64-bit unsigned range (18446744073709551000)
  • v.AsInt64() (int64, bool) — refuses overflow rather than wrapping
  • v.AsFloat64() (float64, bool), v.AsBool() (bool, bool), v.AsString() (string, bool), v.AsTime() (time.Time, bool)
  • v.Kind & Decimal{Mantissa, Scale}.String() for exact money arithmetic