How do I toggle features with flags per user in Go?

Toggling features with flags per user in Go can be accomplished by implementing feature flags. This allows you to enable or disable features dynamically for individual users based on their preferences or conditions. Here's a simple example of how to implement this in Go.


package main

import (
    "fmt"
)

// Feature flags for each user
var featureFlags = map[string]map[string]bool{
    "user1": {"newFeature": true},
    "user2": {"newFeature": false},
}

// Check if a user has access to a feature
func hasFeature(userID string, feature string) bool {
    if features, ok := featureFlags[userID]; ok {
        return features[feature]
    }
    return false
}

func main() {
    // Example usage
    user := "user1"
    feature := "newFeature"

    if hasFeature(user, feature) {
        fmt.Printf("User %s has access to feature %s\n", user, feature)
    } else {
        fmt.Printf("User %s does not have access to feature %s\n", user, feature)
    }
}
    

Go feature flags user-specific features dynamic features Go programming feature toggles