Skip to Content
SDKGoInitialization

Constructor

client := canarygate.New("cg_api_key", canarygate.Options{})

Parameters

ParameterTypeRequiredDescription
apiKeystringYesEnvironment API Key. Created under Settings → API Keys.
optionscanarygate.OptionsNoConfiguration options. See below.

Options

FieldTypeDefaultDescription
BaseURLstringhttp://localhost:3001API base URL (useful for self-hosted environments or proxies).
Environmentstringdefault environmentEnvironment slug, sent via the X-Environment header.
StreamboolfalseOpt in to real-time SSE streaming.
ReconnectDelaytime.Duration5 * time.SecondInitial delay before reconnecting a dropped stream.
MaxReconnectDelaytime.Duration30 * time.SecondMaximum backoff delay for stream reconnection.
HeartbeatTimeoutMstime.Duration65 * time.SecondTime without a server heartbeat before the stream is considered dead and reconnects.

Init()

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

Executes two actions in sequence:

  1. Makes an HTTP fetch to load all flags for the environment (snapshot)
  2. Opens the SSE connection — only when Stream is enabled. Otherwise flags come from the snapshot request only.

Unlike the JavaScript SDK, Init() returns an error if the initial snapshot request fails. After a successful Init(), GetFlag() reads from the local cache; if the connection degrades later, IsStale() reports it and GetFlag() continues to return the last known values.

Examples

Server (HTTP handler with streaming)

package main import ( "log" "net/http" "github.com/canarygate/sdk-go" ) func main() { client := canarygate.New("cg_api_key", canarygate.Options{ Environment: "production", Stream: true, }) if err := client.Init(); err != nil { log.Fatal(err) } defer client.Disconnect() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { flag := client.GetFlag("new-checkout", &canarygate.FlagEvaluationContext{ UserID: "user-42", }) if flag != nil && flag.Enabled { // Serve the new checkout } }) log.Fatal(http.ListenAndServe(":8080", nil)) }

One-shot script (no stream)

client := canarygate.New("cg_api_key", canarygate.Options{}) if err := client.Init(); err != nil { log.Fatal(err) } defer client.Disconnect() flag := client.GetFlag("run-migration", nil) if flag != nil && flag.Enabled { runMigration() }
Last updated on