OpenFeature Provider for Go
Getting started
1. Install the provider
go get github.com/open-feature/go-sdk-contrib/providers/configcat
2. Initialize the provider
The ConfigCat Provider needs a pre-configured ConfigCat Go SDK client:
import (
"context"
sdk "github.com/configcat/go-sdk/v9"
configcat "github.com/open-feature/go-sdk-contrib/providers/configcat/pkg"
"github.com/open-feature/go-sdk/openfeature"
)
// Configure the ConfigCat SDK.
configcatClient := sdk.NewCustomClient(sdk.Config{SDKKey: "#YOUR-SDK-KEY#",
PollingMode: sdk.AutoPoll,
PollInterval: time.Second * 60})
// Configure the provider.
openfeature.SetProvider(configcat.NewProvider(configcatClient))
// Create a client.
client := openfeature.NewClient("app")
For more information about all the configuration options, see the Go SDK documentation.
3. Evaluate your feature flag
isAwesomeFeatureEnabled, err := client.BooleanValue(
context.Background(), "isAwesomeFeatureEnabled", false, openfeature.EvaluationContext{},
)
if err == nil && isAwesomeFeatureEnabled {
doTheNewThing()
} else {
doTheOldThing()
}
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
openfeature.TargetingKey | Identifier | ☑ |
configcat.EmailKey | Email | |
configcat.CountryKey | Country | |
Any other | Custom |
To evaluate feature flags for a context, use the OpenFeature Evaluation API:
registeredAt, _ := time.Parse(time.DateTime, "2023-11-22 12:34:56")
context := openfeature.NewEvaluationContext("#SOME-USER-ID#", map[string]any{
configcat.EmailKey: "[email protected]",
configcat.CountryKey: "CountryID",
"Rating": 4.5,
"RegisteredAt": registeredAt,
"Roles": []string{"Role1","Role2"},
})
isAwesomeFeatureEnabled, err := client.BooleanValue(
context.Background(), "isAwesomeFeatureEnabled", false, context,
)