Go Server SDK Getting Started
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{
EnableEdgeDB: false,
EnableCloudBucketing: false,
EventFlushIntervalMS: 30 * time.Second,
ConfigPollingIntervalMS: 1 * time.Minute,
RequestTimeout: 30 * time.Second,
DisableAutomaticEventLogging: false,
DisableCustomEventLogging: 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")