|
| 1 | +--- |
| 2 | +title: Enable conditional features with a custom filter in a Go Gin web application |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to implement a custom feature filter to enable conditional feature flags for your Go Gin web application. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +ms.devlang: golang |
| 7 | +author: linglingye |
| 8 | +ms.author: linglingye |
| 9 | +ms.topic: how-to |
| 10 | +ms.custom: devx-track-go, mode-other |
| 11 | +ms.date: 07/25/2025 |
| 12 | +--- |
| 13 | + |
| 14 | +# Enable conditional features with a custom filter in a Go Gin web application |
| 15 | + |
| 16 | +Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see [Enable conditional features with feature filters](./howto-feature-filters.md). |
| 17 | + |
| 18 | +The example used in this guide is based on the Go Gin web application introduced in the feature management [quickstart](./quickstart-feature-flag-go-gin.md). Before proceeding further, complete the quickstart to create a Go Gin web application with a *Beta* feature flag. Once completed, you must [add a custom feature filter](./howto-feature-filters.md) to the *Beta* feature flag in your App Configuration store. |
| 19 | + |
| 20 | +In this guide, you'll learn how to implement a custom feature filter and use the feature filter to enable features conditionally. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- Create a [Go Gin web application with a feature flag](./quickstart-feature-flag-go-gin.md). |
| 25 | +- [Add a custom feature filter to the feature flag](./howto-feature-filters.md) |
| 26 | +- [Azure App Configuration Go provider](https://pkg.go.dev/github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration) v1.1.0-beta.1 or later. |
| 27 | + |
| 28 | +## Implement a custom feature filter |
| 29 | + |
| 30 | +You've added a custom feature filter named **Random** with a **Percentage** parameter for your *Beta* feature flag in the prerequisites. Next, you'll implement the feature filter to enable the *Beta* feature flag based on the chance defined by the **Percentage** parameter. |
| 31 | + |
| 32 | +1. Create a `random_filter.go` file with the following code: |
| 33 | + |
| 34 | + ```golang |
| 35 | + package main |
| 36 | + |
| 37 | + import ( |
| 38 | + "fmt" |
| 39 | + "math/rand" |
| 40 | + "time" |
| 41 | + |
| 42 | + "github.com/microsoft/Featuremanagement-Go/featuremanagement" |
| 43 | + ) |
| 44 | + |
| 45 | + type RandomFilter struct{} |
| 46 | + |
| 47 | + func (f *RandomFilter) Name() string { |
| 48 | + return "Random" |
| 49 | + } |
| 50 | + |
| 51 | + func (f *RandomFilter) Evaluate(evalCtx featuremanagement.FeatureFilterEvaluationContext, appCtx any) (bool, error) { |
| 52 | + percentage, ok := evalCtx.Parameters["Percentage"].(float64) |
| 53 | + if !ok { |
| 54 | + return false, fmt.Errorf("invalid parameter type for Percentage: expected float64, got %T", evalCtx.Parameters["Percentage"]) |
| 55 | + } |
| 56 | + |
| 57 | + rand.Seed(time.Now().UnixNano()) |
| 58 | + randomValue := rand.Intn(100) |
| 59 | + return randomValue <= int(percentage), nil |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | + You added a `RandomFilter` struct that implements the `FeatureFilter` interface from the `featuremanagement` library. The `FeatureFilter` interface has two methods: |
| 64 | + - `Name()` returns the filter name **Random**, which matches the filter name you set in the *Beta* feature flag in Azure App Configuration. |
| 65 | + - `Evaluate()` is called whenever a feature flag is evaluated. A feature filter enables a feature flag by returning `true`. |
| 66 | + |
| 67 | +2. Update your `main.go` file to register the `RandomFilter` when creating the feature manager: |
| 68 | + |
| 69 | + ```golang |
| 70 | + // ...existing code... |
| 71 | +
|
| 72 | + func main() { |
| 73 | + ctx := context.Background() |
| 74 | +
|
| 75 | + // Load Azure App Configuration |
| 76 | + appConfig, err := loadAzureAppConfiguration(ctx) |
| 77 | + if err != nil { |
| 78 | + log.Fatalf("Error loading Azure App Configuration: %v", err) |
| 79 | + } |
| 80 | +
|
| 81 | + // Create feature flag provider |
| 82 | + featureFlagProvider, err := azappconfig.NewFeatureFlagProvider(appConfig) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("Error creating feature flag provider: %v", err) |
| 85 | + } |
| 86 | +
|
| 87 | + // Register custom filters |
| 88 | + options := &featuremanagement.Options{ |
| 89 | + Filters: []featuremanagement.FeatureFilter{ |
| 90 | + &RandomFilter{}, |
| 91 | + }, |
| 92 | + } |
| 93 | +
|
| 94 | + // Create feature manager with custom filters |
| 95 | + featureManager, err := featuremanagement.NewFeatureManager(featureFlagProvider, options) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("Error creating feature manager: %v", err) |
| 98 | + } |
| 99 | +
|
| 100 | + // ...existing code... |
| 101 | + } |
| 102 | + ``` |
| 103 | + |
| 104 | +## Feature filter in action |
| 105 | + |
| 106 | +Relaunch the application and refresh the browser a few times. Without manually toggling the feature flag, you will see that the **Beta** menu sometimes appears and sometimes doesn't. |
| 107 | +
|
| 108 | +:::image type="content" source="./media/quickstarts/gin-app-feature-flag-before.png" alt-text="Screenshot of Gin web app with Beta menu hidden."::: |
| 109 | +
|
| 110 | +:::image type="content" source="./media/quickstarts/gin-app-feature-flag-after.png" alt-text="Screenshot of Gin web app with Beta menu."::: |
| 111 | +
|
| 112 | +## Next steps |
| 113 | +
|
| 114 | +To learn more about the built-in feature filters, continue to the following documents. |
| 115 | +
|
| 116 | +> [!div class="nextstepaction"] |
| 117 | +> [Enable features on a schedule](./howto-timewindow-filter.md) |
| 118 | +
|
| 119 | +> [!div class="nextstepaction"] |
| 120 | +> [Roll out features to targeted audience](./howto-targetingfilter.md) |
| 121 | +
|
| 122 | +For the full feature rundown of the Go feature management library, continue to the following document. |
| 123 | +
|
| 124 | +> [!div class="nextstepaction"] |
| 125 | +> [Go Feature Management reference](https://pkg.go.dev/github.com/microsoft/Featuremanagement-Go/featuremanagement) |
0 commit comments