Go
Getting Started:
go
1. Get the SDK with 2. Import the ConfigCat package
3. Create the ConfigCat client with your SDK Key
4. Get your setting value
5. Stop ConfigCat client
You can safely shut down the client instance and release all associated resources on application exit.
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 configuration options
configcat.NewCustomClient(config)
returns a client with custom configuration. The config
parameter is a stucture which
contains the custom configuration.
Available configuration options:
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 | Obsolete 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. |
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. |
PollingMode | configcat.PollingMode | Defaults to AutoPoll . Sets the polling mode for the client. See below. |
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. |
ChangeNotify | func() | An optional callback to invoke when a new configuration has fetched. |
Then you can pass it to the NewCustomClient()
method:
caution
We strongly recommend you to use the ConfigCat Client as a Singleton object in your application
Get[TYPE]Value()
Anatomy of 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.
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. |
User Object
The User Object is essential if you'd like to use ConfigCat's Targeting feature.
Simple user object creation:
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. |
Other options to create a user object:
The ConfigCat SDK uses reflection to determine what attributes are available on a user object. Your can either implement the UserAttributes
interface - then its GetAttribute(string) string
method will be used to retrieve the attributes - or use a struct type where each public field is treated as a possible comparison attribute.
If a field's type implements a String() string
method, the field will be treated as a textual and the String()
method will be called to determine the value.
If a field's type is map[string]string
, the map value 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.
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.
You have the option to configure a ChangeNotify
callback that will be notified when a new configuration is fetched. The policy calls the given method only, when the new configuration is differs from the cached one.
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.
Manual polling
Manual polling gives you full control over when the setting values are downloaded. ConfigCat SDK will not update them automatically. Calling Refresh()
is your application's responsibility.
The setting value retrieval methods will return
defaultValue
if the cache is empty. CallRefresh()
to update the cache.
GetAllKeys()
You can get all the setting keys by calling the GetAllKeys()
method of the ConfigCat Client.
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:
You can define setting descriptors that could be also shared between those components and used for evaluation:
Then you can use the descriptor to retrieve the setting's value from a snapshot:
Also, because of the immutability, snapshots allow safe iterative operations over their setting values avoiding the possibility of data change - caused by e.g. a new configuration download initiated by a get value call - within a loop.
For example, evaluating all setting values for every key could be done safely in the following way:
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:
Then use your custom cache implementation:
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:
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
Available log levels:
Level | Description |
---|---|
ErrorLevel | Only error level events are logged. |
WarnLevel | 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: