| title | Tutorial - Use dynamic configuration in a Go console app |
|---|---|
| titleSuffix | Azure App Configuration |
| description | In this quickstart, learn how to dynamically refresh Azure App Configuration data in a Go console application |
| ms.service | azure-app-configuration |
| services | azure-app-configuration |
| author | linglingye |
| ms.devlang | golang |
| ms.topic | quickstart |
| ms.custom | quickstart, mode-other, devx-track-go |
| ms.date | 05/22/2025 |
| ms.author | linglingye |
In this quickstart, you'll enhance a basic Go console application to dynamically refresh configuration from Azure App Configuration. This allows your application to pick up configuration changes without requiring a restart.
- Complete the Quickstart: Create a Go console app with Azure App Configuration as the starting point for this quickstart
-
Open the file
appconfig.go. Inside theloadAzureAppConfigurationfunction, update theoptionsto enable refresh. Go provider will reload the entire configuration whenever it detects a change in any of the selected key-values (those starting with Config. and having no label). For more information about monitoring configuration changes, see Best practices for configuration refresh.options := &azureappconfiguration.Options{ Selectors: []azureappconfiguration.Selector{ { KeyFilter: "Config.*", }, }, TrimKeyPrefixes: []string{"Config."}, RefreshOptions: azureappconfiguration.KeyValueRefreshOptions{ Enabled: true, }, }
[!TIP] You can set the
Intervalproperty of theRefreshOptionsto specify the minimum time between configuration refreshes. In this example, you use the default value of 30 seconds. Adjust to a higher value if you need to reduce the number of requests made to your App Configuration store. -
Open the file
main.goand add the following code to your main function:// Existing code in main.go // ... ... fmt.Println("\nRaw JSON Configuration:") fmt.Println("------------------------") fmt.Println(string(jsonBytes)) // Register refresh callback to update the configuration provider.OnRefreshSuccess(func() { var updatedConfig Config // Re-unmarshal the configuration err := provider.Unmarshal(&updatedConfig, nil) if err != nil { log.Printf("Error unmarshalling updated configuration: %s", err) return } fmt.Printf("Message: %s\n", updatedConfig.Message) }) // Setup a channel to listen for termination signals done := make(chan os.Signal, 1) signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) fmt.Println("\nWaiting for configuration changes...") fmt.Println("(Update values in Azure App Configuration to see refresh in action)") fmt.Println("Press Ctrl+C to exit") // Start a ticker to periodically trigger refresh ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() // Keep the application running until terminated for { select { case <-ticker.C: // Trigger refresh in background go func() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := provider.Refresh(ctx); err != nil { log.Printf("Error refreshing configuration: %s", err) } }() case <-done: fmt.Println("\nExiting...") return } }
-
Run your application:
go mod tidy go run . -
Keep the application running.
-
Navigate to your App Configuration store and update the value of the
Config.Messagekey.Key Value Content type Config.Message Hello World - updated! Leave empty -
Observe your console application - within 30 seconds, it should detect the change and display the updated configuration.
[!INCLUDE azure-app-configuration-cleanup]