Docs / SDKs / Go Go SDK Thread-safe event client with built-in batching, retries, and graceful shutdown. Zero external dependencies. Requires Go 1.22+.
Installation# bash Copy
go get github.com/segops/sdk-goQuick Start# go Copy
package main
import (
"context"
"log"
"github.com/segops/sdk-go"
)
func main() {
client := segops.New(segops.Options{
APIURL: "https://api.segops.ai",
APIKey: "sk_...",
})
defer func() {
if err := client.Shutdown(context.Background()); err != nil {
log.Println("shutdown:", err)
}
}()
client.Track(segops.Event{
UserID: "user-123",
EventType: "order_placed",
Payload: map[string]any{
"order_id": "ord-456",
"total": 89.95,
},
})
client.Identify(segops.Context{
UserID: "user-123",
Traits: map[string]any{
"email": "[email protected] ",
"plan": "starter",
"company": "Acme Corp",
},
})
}Options# go Copy
type Options struct {
APIURL string // Base URL, e.g. "https://api.segops.ai"
APIKey string // API key (sk_…)
BatchSize int // Default: 20
FlushEvery time.Duration // Default: 5s
MaxRetries int // Default: 3
Logger *log.Logger // Default: log.Default()
}Field Default Description APIURL — Base URL of your SegOps deployment APIKey — API key with ingest permission BatchSize 20 Max events before an automatic flush FlushEvery 5s Periodic flush interval MaxRetries 3 Retry attempts on 5xx (exponential back-off: 0ms, 500ms, 1s) Logger log.Default() Logger for flush errors
Methods# Track(e Event) Enqueue an event. Non-blocking, safe for concurrent use.
go Copy
client.Track(segops.Event{
UserID: "user-123",
EventType: "page_viewed",
// OccurredAt defaults to time.Now() when omitted
Payload: map[string]any{"path": "/home"},
})Identify(ctx Context) Record user traits as a context_identified event.
go Copy
client.Identify(segops.Context{
UserID: "user-123",
Traits: map[string]any{
"email": "[email protected] ",
"plan": "starter",
"company": "Acme Corp",
},
})Flush(ctx context.Context) error Flush all buffered events immediately. Blocks until the HTTP request completes.
go Copy
if err := client.Flush(ctx); err != nil {
log.Println("flush failed:", err)
}Shutdown(ctx context.Context) error Stop the background goroutine and flush any remaining events. Always call before the process exits.
go Copy
defer client.Shutdown(context.Background())Types# go Copy
// Event is a single user event.
type Event struct {
UserID string // Required
EventType string // Required; snake_case
OccurredAt string // Optional; RFC 3339. Defaults to now.
Payload map[string]any // Optional
}
// Context carries user identity / trait updates.
type Context struct {
UserID string
Traits map[string]any
}Error Handling# The background goroutine logs flush errors via the configured Logger. Individual Flush and Shutdown calls return error directly. Transient 5xx responses are retried with exponential back-off: 0 ms, 500 ms, 1 s.