Tutorial - Use dynamic configuration in a Go console app - Azure App Configuration (original) (raw)
In this article
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.
Prerequisites
- Complete the Quickstart: Create a Go console app with Azure App Configuration as the starting point for this quickstart
Reload data from App Configuration
- 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 Interval property of the RefreshOptions to 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.
2. Open the file main.go and 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 the application
- 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.
Clean up resources
If you don't want to continue using the resources created in this article, delete the resource group you created here to avoid charges.
Important
Deleting a resource group is irreversible. The resource group and all the resources in it are permanently deleted. Ensure that you don't accidentally delete the wrong resource group or resources. If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.
- Sign in to the Azure portal, and select Resource groups.
- In the Filter by name box, enter the name of your resource group.
- In the result list, select the resource group name to see an overview.
- Select Delete resource group.
- You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select Delete.
After a few moments, the resource group and all its resources are deleted.
Feedback
Was this page helpful?
No
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
- Last updated on 2025-06-16