Feature Flag Management System in Go
Feature flags (also known as feature toggles) are a powerful technique for enabling or disabling features in your application without deploying new code. This challenge asks you to implement a basic feature flag management system in Go, allowing you to dynamically control feature availability based on a configuration. This is useful for A/B testing, gradual rollouts, and emergency feature disabling.
Problem Description
You need to create a FeatureFlagManager struct in Go that can manage feature flags. The manager should be able to:
- Load Flags: Load feature flag configurations from a simple map. The map keys are feature flag names (strings), and the values are booleans representing whether the feature is enabled (true) or disabled (false).
- Check Flag Status: Provide a method
IsEnabled(flagName string) boolthat returnstrueif the specified feature flag is enabled, andfalseotherwise. If a flag is not found in the configuration, it should be treated as disabled. - Update Flag Status: Provide a method
SetFlag(flagName string, enabled bool)that updates the status of a feature flag. If the flag doesn't exist, it should be created. - Handle Missing Flags Gracefully: The
IsEnabledmethod should returnfalseif a requested flag is not present in the configuration.
Examples
Example 1:
Input:
- Feature flag configuration: map[string]bool{"new_ui": true, "dark_mode": false}
- Call: FeatureFlagManager.IsEnabled("new_ui")
Output: true
Explanation: The "new_ui" flag is enabled in the configuration.
Example 2:
Input:
- Feature flag configuration: map[string]bool{"new_ui": true, "dark_mode": false}
- Call: FeatureFlagManager.IsEnabled("dark_mode")
Output: false
Explanation: The "dark_mode" flag is disabled in the configuration.
Example 3:
Input:
- Feature flag configuration: map[string]bool{"new_ui": true, "dark_mode": false}
- Call: FeatureFlagManager.IsEnabled("experimental_feature")
Output: false
Explanation: The "experimental_feature" flag is not present in the configuration, so it's treated as disabled.
Example 4:
Input:
- Feature flag configuration: map[string]bool{"new_ui": true, "dark_mode": false}
- Call: FeatureFlagManager.SetFlag("dark_mode", true)
- Call: FeatureFlagManager.IsEnabled("dark_mode")
Output: true
Explanation: The "dark_mode" flag is updated to enabled.
Constraints
- The feature flag names will be strings.
- The feature flag values will be booleans.
- The number of feature flags will be relatively small (less than 100). Performance is not a primary concern for this exercise.
- The input configuration map will always be valid (no nil maps).
Notes
- Consider using a
map[string]boolto store the feature flag configurations. - Think about how to handle the case where a flag is not found in the configuration.
- The
SetFlagmethod should modify the internal state of theFeatureFlagManager. - Focus on clarity and correctness over extreme optimization. A simple, readable solution is preferred.
- You do not need to persist the flags to disk. The configuration should live in memory.