Skip to content

Commit 2d27bfa

Browse files
authored
Merge pull request #303307 from linglingye001/go/fm/custom
Go Gin web app in custom filter scenario
2 parents 8a1001d + 020af4b commit 2d27bfa

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@
200200
href: howto-feature-filters-javascript.md
201201
- name: Python
202202
href: howto-feature-filters-python.md
203+
- name: Go Gin
204+
href: howto-feature-filters-go.md
203205
- name: Enable features on a schedule
204206
items:
205207
- name: Overview

articles/azure-app-configuration/concept-feature-management.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ To start using feature flags with Azure App Configuration, continue to the follo
9696
> [!div class="nextstepaction"]
9797
> [JavaScript](./quickstart-feature-flag-javascript.md)
9898
99+
> [!div class="nextstepaction"]
100+
> [Go](./quickstart-feature-flag-go-console.md)
101+
102+
> [!div class="nextstepaction"]
103+
> [Go Gin](./quickstart-feature-flag-go-gin.md)
104+
99105
> [!div class="nextstepaction"]
100106
> [Azure Kubernetes Service](./quickstart-feature-flag-azure-kubernetes-service.md)
101107
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 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+
27+
## Implement a custom feature filter
28+
29+
You 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.
30+
31+
1. Create a `random_filter.go` file with the following code:
32+
33+
```golang
34+
package main
35+
36+
import (
37+
"fmt"
38+
"math/rand"
39+
"time"
40+
41+
"github.com/microsoft/Featuremanagement-Go/featuremanagement"
42+
)
43+
44+
type RandomFilter struct{}
45+
46+
func (f *RandomFilter) Name() string {
47+
return "Random"
48+
}
49+
50+
func (f *RandomFilter) Evaluate(evalCtx featuremanagement.FeatureFilterEvaluationContext, appCtx any) (bool, error) {
51+
percentage, ok := evalCtx.Parameters["Percentage"].(float64)
52+
if !ok {
53+
return false, fmt.Errorf("invalid parameter type for Percentage: expected float64, got %T", evalCtx.Parameters["Percentage"])
54+
}
55+
56+
rand.Seed(time.Now().UnixNano())
57+
randomValue := rand.Intn(100)
58+
return randomValue <= int(percentage), nil
59+
}
60+
```
61+
62+
You added a `RandomFilter` struct that implements the `FeatureFilter` interface from the `featuremanagement` library. The `FeatureFilter` interface has two methods:
63+
- `Name()` returns the filter name **Random**, which matches the filter name you set in the *Beta* feature flag in Azure App Configuration.
64+
- `Evaluate()` is called whenever a feature flag is evaluated. A feature filter enables a feature flag by returning `true`.
65+
66+
2. Update your `main.go` file to register the `RandomFilter` when creating the feature manager:
67+
68+
```golang
69+
// ...existing code...
70+
71+
func main() {
72+
ctx := context.Background()
73+
74+
// Load Azure App Configuration
75+
appConfig, err := loadAzureAppConfiguration(ctx)
76+
if err != nil {
77+
log.Fatalf("Error loading Azure App Configuration: %v", err)
78+
}
79+
80+
// Create feature flag provider
81+
featureFlagProvider, err := azappconfig.NewFeatureFlagProvider(appConfig)
82+
if err != nil {
83+
log.Fatalf("Error creating feature flag provider: %v", err)
84+
}
85+
86+
// Register custom filters
87+
options := &featuremanagement.Options{
88+
Filters: []featuremanagement.FeatureFilter{
89+
&RandomFilter{},
90+
},
91+
}
92+
93+
// Create feature manager with custom filters
94+
featureManager, err := featuremanagement.NewFeatureManager(featureFlagProvider, options)
95+
if err != nil {
96+
log.Fatalf("Error creating feature manager: %v", err)
97+
}
98+
99+
// ...existing code...
100+
}
101+
```
102+
103+
## Feature filter in action
104+
105+
Relaunch the application and refresh the browser a few times. Without manually toggling the feature flag, the **Beta** menu appears randomly based on the percentage you set.
106+
107+
:::image type="content" source="./media/quickstarts/gin-app-feature-flag-before.png" alt-text="Screenshot of Gin web app with Beta menu hidden.":::
108+
109+
:::image type="content" source="./media/quickstarts/gin-app-feature-flag-after.png" alt-text="Screenshot of Gin web app with Beta menu.":::
110+
111+
## Next steps
112+
113+
To learn more about the built-in feature filters, continue to the following documents.
114+
115+
> [!div class="nextstepaction"]
116+
> [Enable features on a schedule](./howto-timewindow-filter.md)
117+
118+
> [!div class="nextstepaction"]
119+
> [Roll out features to targeted audience](./howto-targetingfilter.md)
120+
121+
For the full feature rundown of the Go feature management library, continue to the following document.
122+
123+
> [!div class="nextstepaction"]
124+
> [Go Feature Management reference](https://pkg.go.dev/github.com/microsoft/Featuremanagement-Go/featuremanagement)

articles/azure-app-configuration/howto-feature-filters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You can create custom feature filters that enable features based on your specifi
6262
- [ASP.NET Core](./howto-feature-filters-aspnet-core.md)
6363
- [Node.js](./howto-feature-filters-javascript.md)
6464
- [Python](./howto-feature-filters-python.md)
65+
- [Go Gin](./howto-feature-filters-go.md)
6566

6667
## Next steps
6768

0 commit comments

Comments
 (0)