Go Guide
Installation
bash
cmake -B build -DCMAKE_BUILD_TYPE=Release cmake --build build -j$(nproc) cmake --install build --prefix ~/.local export PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH" export LD_LIBRARY_PATH="$HOME/.local/lib:$LD_LIBRARY_PATH" cd source/bindings/go go build ./...
Config
go
package main
import (
"fmt"
"time"
ddstream "github.com/yourorg/dd_stream/source/bindings/go"
)
func main() {
now := time.Now().Unix()
cfg := ddstream.Config{
APIKey: "YOUR_DD_API_KEY",
AppKey: "YOUR_DD_APP_KEY",
Query: "avg:system.cpu.user{*}",
RangeStart: now - 86400,
RangeEnd: now,
Granularity: 60,
ChunkSeconds: 14400,
EnablePrefetch: true,
}
stream, err := ddstream.New(cfg)
if err != nil {
panic(err)
}
defer stream.Close()
}Zero-Copy Access
Pre-allocate a buffer once and reuse it across calls. The Go binding uses
unsafe.Pointer to cast the C DDPoint array directly into a Go slice —
no allocation in the hot path.
go
buf := make([]ddstream.DataPoint, 50_000)
for {
n, isLast, err := stream.NextBatch(buf)
if err != nil {
fmt.Println("Error:", err)
break
}
if n == 0 {
break
}
// buf[:n] is a zero-copy view into the C++ batch buffer
pts := buf[:n]
// Compute stats
var sumAvg float64
for _, p := range pts {
sumAvg += p.Avg
}
fmt.Printf("Batch: %d points, mean avg=%.2f\n", n, sumAvg/float64(n))
if isLast {
break
}
}Error Handling
NextBatch returns a named (n int, isLast bool, err error) triple. Always
check err != nil before using n. You can also call stream.LastError()
after a failed batch.
go
n, isLast, err := stream.NextBatch(buf)
if err != nil {
fmt.Fprintf(os.Stderr, "stream error: %v\n", err)
// stream.LastError() returns the same string
os.Exit(1)
}
if n == 0 && !isLast {
fmt.Println("Stream exhausted unexpectedly")
}
// Validate credentials before streaming
ok, errMsg := ddstream.Validate(cfg)
if !ok {
fmt.Println("Bad credentials:", errMsg)
}