Skip to main content

Go SDK Reference

Star on GitHub Build Status Go Report Card codecov GoDoc

ConfigCat Go SDK on GitHub

Getting Started:

1. Get the SDK with go

go get github.com/configcat/go-sdk/v7

2. Import the ConfigCat package

import "github.com/configcat/go-sdk/v7"

3. Create the ConfigCat client with your SDK Key

client := configcat.NewClient("<PLACE-YOUR-SDK-KEY-HERE>")

4. Get your setting value

isMyAwesomeFeatureEnabled := client.GetBoolValue("key-of-my-awesome-feature", false, nil)if isMyAwesomeFeatureEnabled {    doTheNewThing()} else {    doTheOldThing()}

5. Stop ConfigCat client

You can safely shut down the client instance and release all associated resources on application exit.

client.Close()

Creating the ConfigCat Client

ConfigCat Client is responsible for:

  • managing the communication between your application and ConfigCat servers.
  • caching your setting values and feature flags.
  • serving values quickly in a failsafe way.

configcat.NewClient(<sdkKey>) returns a client with default options.

ArgumentsDescription
sdkKeySDK Key to access your feature flags and configurations. Get it from ConfigCat Dashboard.

Custom client options

configcat.NewCustomClient(options) returns a customized client. The options parameter is a structure which contains the optional properties.

Available optional properties:

PropertiesTypeDescription
SDKKeystringSDK Key to access your feature flags and configurations. Get it from ConfigCat Dashboard.
DataGovernanceconfigcat.DataGovernanceDefaults to Global. Describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. More about Data Governance. Available options: Global, EuOnly.
BaseUrlstringObsolete Sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the configurations.
Cache ConfigCacheSets a custom cache implementation for the client. See below.
NoWaitForRefreshboolDefaults to false. When it's true the typed get methods (Get[TYPE]Value()) will never wait for a configuration refresh to complete before returning. When it's false and PollingMode is AutoPoll, the first request may block, when PollingMode is Lazy, any request may block.
HttpTimeouttime.DurationSets the maximum wait time for a HTTP response. More about the HTTP timeout
Transporthttp.RoundTripperSets the transport options for the underlying HTTP calls.
Loggerconfigcat.LoggerSets the Logger implementation used by the SDK for logging. More about logging
PollingModeconfigcat.PollingModeDefaults to AutoPoll. Sets the polling mode for the client. More about polling modes.
PollIntervaltime.DurationSets after how much time a configuration is considered stale. When PollingMode is AutoPoll this value is used as the polling rate.
ChangeNotifyfunc()An optional callback to invoke when a new configuration has fetched.
FlagOverrides*configcat.FlagOverridesOptional, sets the local feature flag & setting overrides. More about feature flag overrides.

Then you can pass it to the NewCustomClient() method:

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>", PollingMode: configcat.Manual })
caution

We strongly recommend you to use the ConfigCat Client as a Singleton object in your application. If you want to use multiple SDK Keys in the same application, create only one ConfigCat Client per SDK Key.

Anatomy of Get[TYPE]Value()

Basically all of the value evaluator methods share the same signature, they only differ in their served value type. GetBoolValue() is for evaluating feature flags, GetIntValue() and GetFloatValue() are for numeric and GetStringValue() is for textual settings.

ParametersDescription
keySetting-specific key. Set on ConfigCat Dashboard for each setting.
defaultValueThis value will be returned in case of an error.
userUser Object. Essential when using Targeting. Read more about Targeting.
boolValue := client.GetBoolValue(    "keyOfMyBoolSetting", // Setting Key    false, // Default value    &configcat.UserData{Identifier: "435170f4-8a8b-4b67-a723-505ac7cdea92"} // User Object)
intValue := client.GetIntValue(    "keyOfMyIntSetting", // Setting Key    0, // Default value    &configcat.UserData{Identifier: "435170f4-8a8b-4b67-a723-505ac7cdea92"} // User Object)

User Object

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

Simple user object creation:

user = &configcat.UserData{Identifier: "435170f4-8a8b-4b67-a723-505ac7cdea92"}
user = &configcat.UserData{Identifier: "[email protected]"}

Customized user object creation:

ArgumentsDescription
IdentifierUnique identifier of a user in your application. Can be any value, even an email address.
EmailOptional parameter for easier targeting rule definitions.
CountryOptional parameter for easier targeting rule definitions.
CustomOptional dictionary for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
custom := map[string]string{}custom["SubscriptionType"] = "Pro"custom["UserRole"] = "Admin"user := &configcat.UserData{Identifier: "435170f4-8a8b-4b67-a723-505ac7cdea92",            Email: "[email protected]",            Company: "United Kingdom",            Custom: custom}

Other options to create a user object:

The ConfigCat SDK uses reflection to determine what attributes are available on a user object. You can either implement the UserAttributes interface - which's GetAttribute(string) string method will be used to retrieve the attributes - or use a pointer to a struct type which's public fields are treated as possible comparison attributes.

If a field's type implements a String() string method, the field will be treated as textual and its String() method will be called to determine the value.

If a field's type is map[string]string, the map is used to look up any custom attribute not found directly in the struct. There should be at most one of these fields.

Otherwise, a field type must be a numeric type, a string, a []byte or a github.com/blang/semver.Version.

Polling Modes

The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache then all value retrievals are served from there. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle.
More about polling modes.

Auto polling (default)

The ConfigCat SDK downloads the latest values and stores them automatically every 60 seconds.

Use the the PollInterval option parameter of the ConfigCat Client to change the polling interval.

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    PollingMode: configcat.AutoPoll,    PollInterval: time.Second * 120 /* polling interval in seconds */})

You have the option to set up a ChangeNotify callback that will be notified when a new configuration is fetched. The policy calls the given method only, when the new configuration differs from the cached one.

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    PollingMode: configcat.AutoPoll,    PollInterval: time.Second * 120, /* polling interval in seconds */    ChangeNotify: func() {        // here you can subscribe to configuration changes    }})

Lazy loading

When calling GetBoolValue(), GetIntValue(), GetFloatValue() or GetStringValue() the ConfigCat SDK downloads the latest setting values if they are not present or expired in the cache. In this case, when the NoWaitForRefresh option is false the new setting value will be returned right after the cache update. When it's set to true the setting value retrievals will not wait for the downloads and they will return immediately with the previous setting value.

Use the PollInterval option parameter of the ConfigCat Client to set the cache TTL.

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    PollingMode: configcat.Lazy,    PollInterval: time.Second * 120 /* cache TTL in seconds */})

Manual polling

Manual polling gives you full control over when the config.json (with the setting values) is downloaded. ConfigCat SDK will not update them automatically. Calling Refresh() is your application's responsibility.

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    PollingMode: configcat.Manual})client.Refresh()

The setting value retrieval methods will return defaultValue if the cache is empty. Call Refresh() to update the cache.

GetAllKeys()

You can get all the setting keys by calling the GetAllKeys() method of the ConfigCat Client.

client := configcat.NewClient("#YOUR-SDK-KEY#")keys := client.GetAllKeys()

GetAllValues()

Evaluates and returns the values of all feature flags and settings. Passing a User Object is optional.

client := configcat.NewClient("#YOUR-SDK-KEY#")settingValues := client.GetAllValues(nil)// invoke with user objectuser := &configcat.UserData{Identifier: "435170f4-8a8b-4b67-a723-505ac7cdea92"}settingValuesTargeting := client.GetAllValues(user)

Flag Overrides

With flag overrides you can overwrite the feature flags & settings downloaded from the ConfigCat CDN with local values. Moreover, you can specify how the overrides should apply over the downloaded values. The following 3 behaviours are supported:

  • Local/Offline mode (LocalOnly): When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use all feature flags & settings that are loaded from local-override sources.

  • Local over remote (LocalOverRemote): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the local-override version will take precedence.

  • Remote over local (RemoteOverLocal): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the downloaded version will take precedence.

You can load your feature flag & setting overrides from a file or from a simple map[string]interface{} structure.

JSON File

The SDK can load your feature flag & setting overrides from a file.

client := configcat.NewCustomClient(configcat.Config{    SDKKey: "localhost",    FlagOverrides: &configcat.FlagOverrides{        FilePath: "path/to/local_flags.json",        Behavior: configcat.LocalOnly,    },})

JSON File Structure

The SDK supports 2 types of JSON structures to describe feature flags & settings.

1. Simple (key-value) structure
{  "flags": {    "enabledFeature": true,    "disabledFeature": false,    "intSetting": 5,    "doubleSetting": 3.14,    "stringSetting": "test"  }}

This is the same format that the SDK downloads from the ConfigCat CDN. It allows the usage of all features you can do on the ConfigCat Dashboard.

You can download your current config.json from ConfigCat's CDN and use it as a baseline.

The URL to your current config.json is based on your Data Governance settings:

  • GLOBAL: https://cdn-global.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
  • EU: https://cdn-eu.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
{  "f": {    // list of feature flags & settings    "isFeatureEnabled": {      // key of a particular flag      "v": false, // default value, served when no rules are defined      "i": "430bded3", // variation id (for analytical purposes)      "t": 0, // feature flag's type, possible values:      // 0 -> BOOLEAN      // 1 -> STRING      // 2 -> INT      // 3 -> DOUBLE      "p": [        // list of percentage rules        {          "o": 0, // rule's order          "v": true, // value served when the rule is selected during evaluation          "p": 10, // % value          "i": "bcfb84a7" // variation id (for analytical purposes)        },        {          "o": 1, // rule's order          "v": false, // value served when the rule is selected during evaluation          "p": 90, // % value          "i": "bddac6ae" // variation id (for analytical purposes)        }      ],      "r": [        // list of targeting rules        {          "o": 0, // rule's order          "a": "Identifier", // comparison attribute          "t": 2, // comparator, possible values:          // 0  -> 'IS ONE OF',          // 1  -> 'IS NOT ONE OF',          // 2  -> 'CONTAINS',          // 3  -> 'DOES NOT CONTAIN',          // 4  -> 'IS ONE OF (SemVer)',          // 5  -> 'IS NOT ONE OF (SemVer)',          // 6  -> '< (SemVer)',          // 7  -> '<= (SemVer)',          // 8  -> '> (SemVer)',          // 9  -> '>= (SemVer)',          // 10 -> '= (Number)',          // 11 -> '<> (Number)',          // 12 -> '< (Number)',          // 13 -> '<= (Number)',          // 14 -> '> (Number)',          // 15 -> '>= (Number)',          // 16 -> 'IS ONE OF (Hashed)',          // 17 -> 'IS NOT ONE OF (Hashed)'          "c": "@example.com", // comparison value          "v": true, // value served when the rule is selected during evaluation          "i": "bcfb84a7" // variation id (for analytical purposes)        }      ]    }  }}

Map

You can set up the SDK to load your feature flag & setting overrides from a map[string]interface{}.

client := configcat.NewCustomClient(configcat.Config{    SDKKey: "localhost",    FlagOverrides: &configcat.FlagOverrides{        Values: map[string]interface{}{            "enabledFeature":  true,            "disabledFeature": false,            "intSetting":      5,            "doubleSetting":   3.14,            "stringSetting":   "test",        },        Behavior: configcat.LocalOnly,    },})

Snapshots

A Snapshot represents an immutable state of the given User's current setting values. Because of the immutability they are suitable for sharing between components that rely more on a consistent data state rather than maintaining their own states with individual get setting value calls.

Snapshot creation:

snapshot := client.Snapshot(user)

You can define setting descriptors that could be also shared between those components and used for evaluation:

boolSettingDescriptor := configcat.Bool("keyOfMyBoolSetting" /* Setting Key */, false /* Default value */)

Then you can use the descriptor to retrieve the setting's value from a snapshot:

boolValue := boolSettingDescriptor.Get(snapshot)

Custom Cache

You have the option to inject your custom cache implementation into the client. All you have to do is to satisfy the ConfigCache interface:

type CustomCache struct {}func (cache *CustomCache) Get(ctx context.Context, key string) ([]byte, error) {    // here you have to return with the cached value}func (cache *CustomCache) Set(ctx context.Context, key string, value []byte) error {    // here you have to store the new value in the cache}

Then use your custom cache implementation:

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    Cache: CustomCache{}})

Force refresh

Any time you want to refresh the cached configuration with the latest one, you can call the Refresh() method of the library, which will initiate a new fetch and will update the local cache.

You can also use the RefreshIfOlder() variant when you want to add expiration time windows for local cache updates.

HTTP Proxy

You can use the Transport config option to set up http transport related (like proxy) settings for the http client used by the SDK:

proxyURL, _ := url.Parse("<PROXY-URL>")client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    Transport: &http.Transport{        Proxy: http.ProxyURL(proxyURL),    }})

HTTP Timeout

You can set the maximum wait time for a ConfigCat HTTP response.

client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    HTTPTimeout: time.Second * 10})

Logging

The default logger used by the SDK is logrus, but you have the option to override it with your logger via the Logger config option, it only has to satisfy the Logger interface:

Setting log levels

Using logrus

import {    "github.com/configcat/go-sdk/v7"    "github.com/sirupsen/logrus"}logger := logrus.New()logger.SetLevel(logrus.InfoLevel)client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    Logger: logger})

Using the default logger

import {    "github.com/configcat/go-sdk/v7"    "github.com/sirupsen/logrus"}client := configcat.NewCustomClient(configcat.Config{SDKKey: "<PLACE-YOUR-SDK-KEY-HERE>",    Logger: configcat.DefaultLogger(configcat.LogLevelInfo)})

Available log levels:

LevelDescription
ErrorLevelOnly error level events are logged.
WarnLevelDefault, Errors and Warnings are logged.
InfoLevelErrors, Warnings and feature flag evaluation is logged.
DebugLevelAll of the above plus debug info is logged.

Info level logging helps to inspect the feature flag evaluation process:

ConfigCat - INFO - Evaluate 'isPOCFeatureEnabled'INFO[0000] Evaluating rule: [Email:] [CONTAINS] [@something.com] => no matchINFO[0000] Evaluating rule: [Email:] [CONTAINS] [@example.com] => no matchINFO[0000] Returning false.

Sample Applications

Look under the hood