Skip to content

Configuration

go-feature-flag needs to be initialized to be used.
During the initialization you must give a ffclient.Config{} configuration object.

ffclient.Config{} is the only location where you can put the configuration.

Configuration fields

Field Description
Retriever The configuration retriever you want to use to get your flag file
See Store your flag file for the configuration details.
Context (optional)
The context used by the retriever.
Default: context.Background()
Environment (optional)
The environment the app is running under, can be checked in feature flag rules.
Default: ""
Check "environments" section to understand how to use this parameter.
DataExporter (optional)
DataExporter defines how to export data on how your flags are used.
see export data section for more details.
FileFormat (optional)
Format of your configuration file. Available formats are yaml, toml and json, if you omit the field it will try to unmarshal the file as a yaml file.
Default: YAML
Logger (optional)
Logger used to log what go-feature-flag is doing.
If no logger is provided the module will not log anything.
Default: No log
Notifiers (optional)
List of notifiers to call when your flag file has been changed.
See notifiers section for more details.
PollingInterval (optional) Duration to wait before refreshing the flags.
The minimum polling interval is 1 second.
Default: 60 * time.Second
StartWithRetrieverError (optional) If true, the SDK will start even if we did not get any flags from the retriever. It will serve only default values until the retriever returns the flags.
The init method will not return any error if the flag file is unreachable.
Default: false
Offline (optional) If true, the SDK will not try to retrieve the flag file and will not export any data. No notification will be send neither.
Default: false

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ffclient.Init(ffclient.Config{ 
    PollingInterval:   3 * time.Second,
    Logger:         log.New(file, "/tmp/log", 0),
    Context:        context.Background(),
    Environment:    os.Getenv("MYAPP_ENV"),
    Retriever:      &fileretriever.Retriever{Path: "testdata/flag-config.yaml"},
    FileFormat:     "yaml",
    Notifiers: []notifier.Notifier{
        &webhooknotifier.Notifier{
            EndpointURL: " https://example.com/hook",
            Secret:     "Secret",
            Meta: map[string]string{
                "app.name": "my app",
            },
        },
    },
    DataExporter: ffclient.DataExporter{
        FlushInterval:   10 * time.Second,
        MaxEventInMemory: 1000,
        Exporter: &file.Exporter{
            OutputDir: "/output-data/",
        },
    },
    StartWithRetrieverError: false,
})

Multiple configuration flag files

go-feature-flag comes ready to use out of the box by calling the Init function and it will be available everywhere.
Since most applications will want to use a single central flag configuration, the package provides this. It is similar to a singleton.

In all the examples above, they demonstrate using go-feature-flag in its singleton style approach.

Working with multiple go-feature-flag

You can also create many go-feature-flag clients to use in your application.

Each will have its own unique set of configurations and flags. Each can read from a different config file and from different places.
All the functions that go-feature-flag package supports are mirrored as methods on a GoFeatureFlag.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
x, err := ffclient.New(Config{ Retriever: &httpretriever.Retriever{{URL: "http://example.com/flag-config.yaml",}})
defer x.Close()

y, err := ffclient.New(Config{ Retriever: &httpretriever.Retriever{{URL: "http://example.com/test2.yaml",}})
defer y.Close()

user := ffuser.NewUser("user-key")
x.BoolVariation("test-flag", user, false)
y.BoolVariation("test-flag", user, false)

// ...

When working with multiple GoFeatureFlag, it is up to the user to keep track of the different GoFeatureFlag instances.

Offline mode

In some situations, you might want to stop making remote calls and fall back to default values for your feature flags.
For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise.

You can do this by setting Offline mode in the client's Config.

Advanced configuration