Skip to main content

Go Server SDK Getting Started

GitHub

Initializing SDK

When initializing the Go SDK, you can choose to use Cloud or Local bucketing. The default mode is Local. To use Cloud bucketing, set the devcycle.Options setting EnableCloudBucketing to true.

package main

import (
"log"
"os"
"time"

devcycle "github.com/devcyclehq/go-server-sdk/v2"
)

func main() {
sdkKey := os.Getenv("DEVCYCLE_SERVER_SDK_KEY")

options := devcycle.Options{
EnableCloudBucketing: false,
}

devcycleClient, err := devcycle.NewClient(sdkKey, &options)
if err != nil {
log.Fatalf("Error initializing DevCycle client: %v", err)
}
}

If using local bucketing, be sure to check the error return from creating a new Client - if the local bucketing engine fails to initialize for any reason- it'll return as an error here.

Async Initialization

Additionally, local bucketing mode supports an optional ClientEventHandler parameter which will tell the sdk to run the initialization process in a separate go routine. This can be useful if you want to wait for the client to be fully initialized before proceeding.

onInitializedChannel := make(chan api.ClientEvent)
options := devcycle.Options{
ClientEventHandler: onInitializedChannel,
// other options omitted for this example
}

devcycleClient, err := devcycle.NewClient(sdkKey, &options)
if err != nil {
// handle client initialization error
}

// At this point, the client can be safely used, but might not have downloaded configuration yet and will return default values until that completes
log.Println("DevCycle client not guaranteed to be initialized yet")

<-onInitializedChannel
log.Println("Devcycle client initialized")

Initialization Options

The SDK exposes various initialization options which can be set when initializing the DevCycle client:

package main

import (
"log"
"os"
"time"

devcycle "github.com/devcyclehq/go-server-sdk/v2"
)

func main() {
sdkKey := os.Getenv("DEVCYCLE_SERVER_SDK_KEY")

options := devcycle.Options{
// Insert Options
}

devcycleClient, err := devcycle.NewClient(sdkKey, &options)
if err != nil {
log.Fatalf("Error initializing DevCycle client: %v", err)
}
}
DevCycle OptionTypeDescription
Loggerutil.LoggerLogger override to replace default logger
EnableEdgeDBboolEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
NOTE: This is only available with Cloud Bucketing enabled.
EnableCloudBucketingboolSwitches the SDK to use Cloud Bucketing (via the DevCycle Bucketing API) instead of Local Bucketing.
EventFlushIntervalMStime.DurationControls the interval between flushing events to the DevCycle servers, defaults to 30 seconds.
ConfigPollingIntervalMStime.DurationControls the polling interval in milliseconds to fetch new environment config changes, defaults to 10 seconds, minimum value is 1 second.
RequestTimeouttime.DurationControls the request timeout to fetch new environment config changes, defaults to 5 seconds, must be less than the configPollingIntervalMS value, minimum value is 1 second.
DisableAutomaticEventLoggingboolDisables logging of sdk generated events (e.g. aggVariableEvaluated, aggVariableDefaulted) to DevCycle.
DisableCustomEventLoggingboolDisables logging of custom events, from track() method, and user data to DevCycle.
DisableETagMatchingboolContact Support for usage instructions.
DisableRealtimeUpdatesboolDisables the usage of realtime updates SSE connections for DevCycle, will revert to polling against the config CDN.
MaxEventQueueSizeintControls the maximum size the event queue can grow to until events are dropped. Defaults to 2000.
FlushEventQueueSizeintControls the maximum size the event queue can grow to until a flush is forced. Defaults to 1000.
ConfigCDNURIstringContact support for usage instructions.
EventsAPIURIstringContact support for usage instructions.
ClientEventHandlerapi.ClientEventAsync initialization callback handler.
BucketingAPIURIstringContact support for usage instructions.