|
| 1 | +--- |
| 2 | +title: Quickstart for adding feature flags to Go console apps |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn to implement feature flags in your Go console application using feature management and Azure App Configuration. Dynamically manage feature rollouts and control feature visibility without redeploying the app. |
| 5 | +services: azure-app-configuration |
| 6 | +author: linglingye |
| 7 | +ms.service: azure-app-configuration |
| 8 | +ms.devlang: golang |
| 9 | +ms.topic: quickstart |
| 10 | +ms.custom: devx-track-go, mode-other |
| 11 | +ms.date: 07/03/2025 |
| 12 | +ms.author: linglingye |
| 13 | +#Customer intent: As a Go developer, I want to use feature flags to control feature availability quickly and confidently. |
| 14 | +--- |
| 15 | + |
| 16 | +# Quickstart: Add feature flags to a Go console app |
| 17 | + |
| 18 | +In this quickstart, you'll create a feature flag in Azure App Configuration and use it to dynamically control the availability of features in a Go console app. |
| 19 | + |
| 20 | +The feature management support extends the dynamic configuration feature in App Configuration. This example demonstrates how to integrate feature flags into a Go console application with real-time monitoring capabilities. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/). |
| 25 | +- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store). |
| 26 | +- Go 1.18 or later. For information on installing Go, see the [Go downloads page](https://golang.org/dl/). |
| 27 | +- [Azure App Configuration Go provider](https://pkg.go.dev/github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration) v1.1.0-beta.1 or later. |
| 28 | + |
| 29 | +## Create a feature flag |
| 30 | + |
| 31 | +Add a feature flag called *Beta* to the App Configuration store and leave **Label** and **Description** with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to [Create a feature flag](./manage-feature-flags.md#create-a-feature-flag). |
| 32 | + |
| 33 | +> [!div class="mx-imgBorder"] |
| 34 | +>  |
| 35 | +
|
| 36 | +## Use a feature flag |
| 37 | + |
| 38 | +1. Create a new directory for your Go project and navigate into it: |
| 39 | + |
| 40 | + ```console |
| 41 | + mkdir go-feature-flag-quickstart |
| 42 | + cd go-feature-flag-quickstart |
| 43 | + ``` |
| 44 | + |
| 45 | +1. Initialize a new Go module: |
| 46 | + |
| 47 | + ```console |
| 48 | + go mod init go-feature-flag-quickstart |
| 49 | + ``` |
| 50 | + |
| 51 | +1. Install the required Go packages for Azure App Configuration and feature management: |
| 52 | + |
| 53 | + ```console |
| 54 | + go get github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration |
| 55 | + go get github.com/microsoft/Featuremanagement-Go/featuremanagement |
| 56 | + go get github.com/microsoft/Featuremanagement-Go/featuremanagement/providers/azappconfig |
| 57 | + ``` |
| 58 | + |
| 59 | +1. Update the `options` to enable feature flags in [previous step](./quickstart-go-console-app.md#connect-to-app-configuration). |
| 60 | + |
| 61 | + ```golang |
| 62 | + options := &azureappconfiguration.Options{ |
| 63 | + FeatureFlagOptions: azureappconfiguration.FeatureFlagOptions{ |
| 64 | + Enabled: true, |
| 65 | + RefreshOptions: azureappconfiguration.RefreshOptions{ |
| 66 | + Enabled: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | + > [!TIP] |
| 73 | + > When no selector is specified in `FeatureFlagOptions`, it loads *all* feature flags with *no label* in your App Configuration store. The default refresh interval of feature flags is 30 seconds. You can customize this behavior via the `RefreshOptions` parameter. For example, the following code snippet loads only feature flags that start with *TestApp:* in their *key name* and have the label *dev*. The code also changes the refresh interval time to 5 minutes. Note that this refresh interval time is separate from that for regular key-values. |
| 74 | + > |
| 75 | + > ```golang |
| 76 | + > FeatureFlagOptions{ |
| 77 | + > Enabled: true, |
| 78 | + > Selectors: []azureappconfiguration.Selector{ |
| 79 | + > { |
| 80 | + > KeyFilter: "TestApp:*", |
| 81 | + > LabelFilter: "dev", |
| 82 | + > }, |
| 83 | + > }, |
| 84 | + > RefreshOptions: azureappconfiguration.RefreshOptions{ |
| 85 | + > Enabled: true, |
| 86 | + > Interval: 5 * time.Minute, |
| 87 | + > }, |
| 88 | + > } |
| 89 | + > ``` |
| 90 | + |
| 91 | +1. Create the main application file `main.go`: |
| 92 | + |
| 93 | + ```golang |
| 94 | + package main |
| 95 | + |
| 96 | + import ( |
| 97 | + "context" |
| 98 | + "fmt" |
| 99 | + "log" |
| 100 | + "os" |
| 101 | + "time" |
| 102 | + |
| 103 | + "github.com/microsoft/Featuremanagement-Go/featuremanagement" |
| 104 | + "github.com/microsoft/Featuremanagement-Go/featuremanagement/providers/azappconfig" |
| 105 | + ) |
| 106 | + |
| 107 | + func main() { |
| 108 | + ctx := context.Background() |
| 109 | + |
| 110 | + // Load Azure App Configuration |
| 111 | + appConfig, err := loadAzureAppConfiguration(ctx) |
| 112 | + if err != nil { |
| 113 | + log.Fatalf("Error loading Azure App Configuration: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Create feature flag provider |
| 117 | + featureFlagProvider, err := azappconfig.NewFeatureFlagProvider(appConfig) |
| 118 | + if err != nil { |
| 119 | + log.Fatalf("Error creating feature flag provider: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + // Create feature manager |
| 123 | + featureManager, err := featuremanagement.NewFeatureManager(featureFlagProvider, nil) |
| 124 | + if err != nil { |
| 125 | + log.Fatalf("Error creating feature manager: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + // Monitor the Beta feature flag |
| 129 | + fmt.Println("Monitoring 'Beta' feature flag (press Ctrl+C to exit):") |
| 130 | + fmt.Println("Toggle the Beta feature flag in Azure portal to see real-time updates...") |
| 131 | + fmt.Println() |
| 132 | + |
| 133 | + ticker := time.NewTicker(5 * time.Second) |
| 134 | + defer ticker.Stop() |
| 135 | + |
| 136 | + for { |
| 137 | + select { |
| 138 | + case <-ticker.C: |
| 139 | + // Refresh configuration to get latest feature flag settings |
| 140 | + if err := appConfig.Refresh(ctx); err != nil { |
| 141 | + log.Printf("Error refreshing configuration: %v", err) |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + // Evaluate the Beta feature flag |
| 146 | + isEnabled, err := featureManager.IsEnabled("Beta") |
| 147 | + if err != nil { |
| 148 | + log.Printf("Error checking if Beta feature is enabled: %v", err) |
| 149 | + continue |
| 150 | + } |
| 151 | + |
| 152 | + // Print timestamp and feature status |
| 153 | + timestamp := time.Now().Format("15:04:05") |
| 154 | + fmt.Printf("[%s] Beta is enabled: %t\n", timestamp, isEnabled) |
| 155 | + |
| 156 | + case <-ctx.Done(): |
| 157 | + fmt.Println("\nShutting down...") |
| 158 | + return |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + ``` |
| 163 | + |
| 164 | +## Run the application |
| 165 | + |
| 166 | +1. Run the application: |
| 167 | + |
| 168 | + ```console |
| 169 | + go run main.go |
| 170 | + ``` |
| 171 | + --- |
| 172 | + |
| 173 | +1. The application will start monitoring the *Beta* feature flag and display its current state every 5 seconds: |
| 174 | + |
| 175 | + ```console |
| 176 | + Monitoring 'Beta' feature flag (press Ctrl+C to exit): |
| 177 | + Toggle the Beta feature flag in Azure portal to see real-time updates... |
| 178 | + |
| 179 | + [14:30:15] Beta is enabled: false |
| 180 | + [14:30:20] Beta is enabled: false |
| 181 | + [14:30:25] Beta is enabled: false |
| 182 | + ``` |
| 183 | + |
| 184 | +1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the App Configuration store that you created previously. |
| 185 | + |
| 186 | +1. Select **Feature manager** and locate the *Beta* feature flag. Enable the flag by selecting the checkbox under **Enabled**. |
| 187 | + |
| 188 | +1. Return to your console application. After a few seconds, you should see the feature flag status change: |
| 189 | + |
| 190 | + ```console |
| 191 | + [14:30:30] Beta is enabled: false |
| 192 | + [14:30:35] Beta is enabled: true |
| 193 | + [14:30:40] Beta is enabled: true |
| 194 | + ``` |
| 195 | + |
| 196 | +1. You can toggle the feature flag on and off in the Azure portal to see real-time updates in your console application without restarting it. |
| 197 | + |
| 198 | +1. Press **Ctrl+C** to stop the application. |
| 199 | + |
| 200 | +## Clean up resources |
| 201 | + |
| 202 | +[!INCLUDE[Azure App Configuration cleanup](../../includes/azure-app-configuration-cleanup.md)] |
| 203 | + |
| 204 | +## Next steps |
| 205 | + |
| 206 | +In this quickstart, you created a feature flag in Azure App Configuration and used it in a Go console application. The [Feature Management Go library](https://github.com/microsoft/FeatureManagement-Go) provides rich feature flag capabilities that integrate seamlessly with Azure App Configuration. For more features, continue to the following document. |
| 207 | + |
| 208 | +> [!div class="nextstepaction"] |
| 209 | +> [Go Feature Management reference](https://pkg.go.dev/github.com/microsoft/Featuremanagement-Go/featuremanagement) |
| 210 | + |
| 211 | +While a feature flag allows you to activate or deactivate functionality in your app, you may want to customize a feature flag based on your app's logic. Feature filters allow you to enable a feature flag conditionally. For more information, continue to the following tutorial. |
| 212 | + |
| 213 | +> [!div class="nextstepaction"] |
| 214 | +> [Enable conditional features with feature filters](./howto-feature-filters.md) |
| 215 | + |
| 216 | +Azure App Configuration offers built-in feature filters that enable you to activate a feature flag only during a specific period or to a particular targeted audience of your app. For more information, continue to the following tutorial. |
| 217 | + |
| 218 | +> [!div class="nextstepaction"] |
| 219 | +> [Enable features on a schedule](./howto-timewindow-filter.md) |
| 220 | + |
| 221 | +> [!div class="nextstepaction"] |
| 222 | +> [Roll out features to targeted audiences](./howto-targetingfilter.md) |
| 223 | + |
0 commit comments