Go SDK Reference
This documentation applies to the v8.x version of the ConfigCat Go SDK. For the documentation of the latest release, please refer to this page.
Getting Started
1. Get the SDK with go
go get github.com/configcat/go-sdk/v8
2. Import the ConfigCat package
import "github.com/configcat/go-sdk/v8"
3. Create the ConfigCat client with your SDK Key
client := configcat.NewClient("#YOUR-SDK-KEY#")
4. Get your setting value
isMyAwesomeFeatureEnabled := client.GetBoolValue("isMyAwesomeFeatureEnabled", 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.
Arguments | Description |
---|---|
sdkKey | SDK 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:
Properties | Type | Description |
---|---|---|
SDKKey | string | SDK Key to access your feature flags and configurations. Get it from ConfigCat Dashboard. |
DataGovernance | configcat.DataGovernance | Defaults 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 . |
BaseUrl | string | Sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the configurations. |
Cache | ConfigCache | Sets a custom cache implementation for the client. See below. |
NoWaitForRefresh | bool | Defaults 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. |
HttpTimeout | time.Duration | Sets the maximum wait time for a HTTP response. More about the HTTP timeout |
Transport | http.RoundTripper | Sets the transport options for the underlying HTTP calls. |
Logger | configcat.Logger | Sets the Logger implementation used by the SDK for logging. More about logging |
PollingMode | configcat.PollingMode | Defaults to AutoPoll . Sets the polling mode for the client. More about polling modes. |
PollInterval | time.Duration | Sets after how much time a configuration is considered stale. When PollingMode is AutoPoll this value is used as the polling rate. |
FlagOverrides | *configcat.FlagOverrides | Sets the local feature flag & setting overrides. More about feature flag overrides. |
DefaultUser | configcat.User | Sets the default user. More about default user. |
Offline | bool | Defaults to false . Indicates whether the SDK should be initialized in offline mode. More about offline mode. |
Hooks | *configcat.Hooks | Used to subscribe events that the SDK sends in specific scenarios. More about hooks. |
Then you can pass it to the NewCustomClient()
method:
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
PollingMode: configcat.Manual,
Logger: configcat.DefaultLogger(configcat.LogLevelInfo)})
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()
All feature flag 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.
Parameters | Description |
---|---|
key | Setting-specific key. Set on ConfigCat Dashboard for each setting. |
defaultValue | This value will be returned in case of an error. |
user | User Object. Essential when using Targeting. Read more about Targeting. |
boolValue := client.GetBoolValue(
"keyOfMyBoolSetting", // Setting Key
false, // Default value
&configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"} // User Object
)
intValue := client.GetIntValue(
"keyOfMyIntSetting", // Setting Key
0, // Default value
&configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"} // User Object
)
Anatomy of Get[TYPE]ValueDetails()
GetValueDetails()
is similar to GetValue()
but instead of returning the evaluated value only, it gives more detailed information about the evaluation result.
Parameters | Description |
---|---|
key | Setting-specific key. Set on ConfigCat Dashboard for each setting. |
defaultValue | This value will be returned in case of an error. |
user | User Object. Essential when using Targeting. Read more about Targeting. |
details := client.GetBoolValueDetails(
"keyOfMyBoolSetting", // Setting Key
false, // Default value
&configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"} // User Object
)
details := client.GetIntValueDetails(
"keyOfMyIntSetting", // Setting Key
0, // Default value
&configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"} // User Object
)
The details
result contains the following information:
Field | Type | Description |
---|---|---|
Value | bool / string / int / float64 | The evaluated value of the feature flag or setting. |
Data.Key | string | The key of the evaluated feature flag or setting. |
Data.IsDefaultValue | bool | True when the default value passed to Get[TYPE]ValueDetails() is returned due to an error. |
Data.Error | error | In case of an error, this field contains the error message. |
Data.User | User | The User Object that was used for evaluation. |
Data.MatchedEvaluationPercentageRule | *PercentageRule | If the evaluation was based on a Percentage Rule, this field contains that specific rule. |
Data.MatchedEvaluationRule | *RolloutRule | If the evaluation was based on a Targeting Rule, this field contains that specific rule. |
Data.FetchTime | time.Time | The last download time of the current config. |
User Object
The User Object is essential if you'd like to use ConfigCat's Targeting feature.
user = &configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"}
user = &configcat.UserData{Identifier: "[email protected]"}
Customized User Object creation
Arguments | Description |
---|---|
Identifier | Unique identifier of a user in your application. Can be any value, even an email address. |
Email | Optional parameter for easier Targeting Rule definitions. |
Country | Optional parameter for easier Targeting Rule definitions. |
Custom | Optional 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: "#UNIQUE-USER-IDENTIFIER#",
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
.
Default user
There's an option to set a default User Object that will be used at feature flag and setting evaluation. It can be useful when your application has a single user only, or rarely switches users.
You can set the default User Object on SDK initialization:
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
DefaultUser: &configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"}})
Whenever the Get[TYPE]Value()
, Get[TYPE]ValueDetails()
, GetAllValues()
, or GetAllValueDetails()
methods are called without an explicit user
parameter, the SDK will automatically use the default user as a User Object.
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
DefaultUser: &configcat.UserData{Identifier: "[email protected]"}})
// The default user will be used at the evaluation process.
value := client.GetBoolValue("keyOfMyBoolSetting", false, nil)
When the user
parameter is specified on the requesting method, it takes precedence over the default user.
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
DefaultUser: &configcat.UserData{Identifier: "[email protected]"}})
otherUser = &configcat.UserData{Identifier: "[email protected]"}
// otherUser will be used at the evaluation process.
value := client.GetBoolValue("keyOfMyBoolSetting", false, otherUser)
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: "#YOUR-SDK-KEY#",
PollingMode: configcat.AutoPoll,
PollInterval: time.Second * 120 /* polling interval in seconds */})
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: "#YOUR-SDK-KEY#",
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: "#YOUR-SDK-KEY#",
PollingMode: configcat.Manual})
client.Refresh()
The setting value retrieval methods will return
defaultValue
if the cache is empty. CallRefresh()
to update the cache.
Hooks
With the following hooks you can subscribe to particular events fired by the SDK:
-
OnConfigChanged()
: This event is sent when the SDK loads a new config JSON into memory from cache or via HTTP. -
OnFlagEvaluated(EvaluationDetails)
: This event is sent each time when the SDK evaluates a feature flag or setting. The event sends the same evaluation details that you would get fromGet[TYPE]ValueDetails()
. -
OnError(error)
: This event is sent when an error occurs within the ConfigCat SDK.
You can subscribe to these events on SDK initialization:
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
Hooks: &configcat.Hooks{OnFlagEvaluated: func(details *configcat.EvaluationDetails) {
/* handle the event */
}}})
Online / Offline mode
In cases when you'd want to prevent the SDK from making HTTP calls, you can put it in offline mode:
client.SetOffline()
In offline mode, the SDK won't initiate HTTP requests and will work only from its cache.
To put the SDK back in online mode, you can do the following:
client.SetOnline()
With
client.IsOffline()
you can check whether the SDK is in offline mode.
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 Object
user := &configcat.UserData{Identifier: "#UNIQUE-USER-IDENTIFIER#"}
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 only (
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"
}
}
2. Complex (full-featured) structure
This is the same format that the SDK downloads from the ConfigCat CDN. It allows the usage of all features that are available 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
The ConfigCat SDK stores the downloaded config data in a local cache to minimize network traffic and enhance client performance.
If you prefer to use your own cache solution, such as an external or distributed cache in your system,
you can implement 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: "#YOUR-SDK-KEY#",
Cache: CustomCache{}})
The Go SDK supports shared caching. You can read more about this feature and the required minimum SDK versions here.
Force refresh
Call the Refresh()
method on the client to download the latest config JSON and update the 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: "#YOUR-SDK-KEY#",
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: "#YOUR-SDK-KEY#",
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/v8"
"github.com/sirupsen/logrus"
}
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
Logger: logger})
Using the default logger
import {
"github.com/configcat/go-sdk/v8"
"github.com/sirupsen/logrus"
}
client := configcat.NewCustomClient(configcat.Config{SDKKey: "#YOUR-SDK-KEY#",
Logger: configcat.DefaultLogger(configcat.LogLevelInfo)})
Available log levels:
Level | Description |
---|---|
ErrorLevel | Only error level events are logged. |
WarnLevel | Default, Errors and Warnings are logged. |
InfoLevel | Errors, Warnings and feature flag evaluation is logged. |
DebugLevel | All 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 match
INFO[0000] Evaluating rule: [Email:] [CONTAINS] [@example.com] => no match
INFO[0000] Returning false.