Ahmed Ghazey
4 min readMar 5, 2023

Feature Flag with Golang

Feature flags, also known as feature toggles, are a software development technique that allows developers to turn features on and off in a production environment without requiring a new deployment. Feature flags are commonly used for A/B testing, canary releases, and gradual rollouts.

There are several ways to enable/disable features in a Golang application, but one popular approach is to use feature flags.

Some popular options include:

  1. FeatureToggle: This library provides a simple way to enable/disable features based on environment variables, configuration files, or command-line flags.
  2. Go-FF: This library provides a simple and flexible way to manage feature flags in your Golang application. It supports multiple backends, including in-memory, file-based, and Redis-based feature flag stores.
  3. Unleash-client-go: This library is a client for the Unleash feature flagging platform, which provides a centralized way to manage feature flags for multiple applications.
  4. Flagr: This library is a feature flagging and A/B testing platform that provides a REST API for managing feature flags. It also includes a Golang client library for interacting with the API.

You can choose the library that best fits your needs based on the complexity of your application and the specific features you want to enable/disable.

Here is an example of a FeatureToggle library:

package main

import (
"fmt"
"github.com/nicholasvuono/feature-toggle"
)

func main() {
// Define a feature toggle for the new feature
newFeatureToggle := featuretoggle.NewToggle("new-feature", false)

// Use the feature toggle to conditionally execute code
if newFeatureToggle.Enabled() {
// Execute code for new feature
fmt.Println("New feature is enabled!")
} else {
// Execute default code
fmt.Println("New feature is disabled.")
}
}

In the example above, we use the FeatureToggle library to define and manage a feature toggle for a new feature in our application. We create a new toggle using the “NewToggle” method and set its initial value to “false”.

We then use the “Enabled” method of the feature toggle to execute code conditionally. If the feature toggle is enabled, we execute code for the new feature, otherwise, we execute the default code.

To use this library, you need to import the “github.com/nicholasvuono/feature-toggle” package and install it using the “go get” command:

go get github.com/nicholasvuono/feature-toggle

You can then use the FeatureToggle library in your Golang application to implement feature toggles for your features. This library provides an easy-to-use interface for defining and managing feature toggles, making it simple to experiment with new features in a controlled manner.

Another example using the GFlag library

package main

import (
"flag"
"fmt"
)

func main() {
// Define a command-line flag for the feature toggle
featureToggle := flag.Bool("new-feature", false, "Enable new feature")

// Parse the command-line flags
flag.Parse()

// Use the feature toggle to conditionally execute code
if *featureToggle {
// Execute code for new feature
fmt.Println("New feature is enabled!")
} else {
// Execute default code
fmt.Println("New feature is disabled.")
}
}

In the example above, we are defining a command-line flag for a feature toggle using the GFlags library. The “Bool” method is used to define a boolean flag with a default value of “false” and a usage message.

We then parse the command-line flags using the “Parse” method, which reads the flag values from the command-line arguments.

Finally, we use the feature toggle value to execute code conditionally. If the feature toggle is set to true, we execute code for the new feature, otherwise, we execute default code.

To run this example, you can compile the program and pass the “new-feature” flag as a command-line argument:

go build featuretoggle.go ./featuretoggle -new-feature=true

Here’s an example of how to use the Unleash client library in Golang to implement feature toggles:

package main

import (
"fmt"
"github.com/Unleash/unleash-client-go/v3"
"github.com/Unleash/unleash-client-go/v3/api"
"time"
)

func main() {
// Define your Unleash configuration
config := unleash.ClientConfig{
AppName: "my-go-app",
InstanceID: "instance-1",
URL: "https://my-unleash-server.com/api",
RefreshInterval: time.Second * 15,
MetricsInterval: time.Second * 30,
}

// Create a new Unleash client instance
client, err := unleash.NewClient(config)
if err != nil {
panic(err)
}

// Start the Unleash client
defer client.Close()
client.Start()

// Check if the new feature is enabled
isEnabled := client.IsEnabled("new-feature", unleash.WithDefault(false))

// Use the feature toggle to conditionally execute code
if isEnabled {
// Execute code for new feature
fmt.Println("New feature is enabled!")
} else {
// Execute default code
fmt.Println("New feature is disabled.")
}
}

In the example above, we are defining our Unleash client configuration, including the name of our application, instance ID, and the URL of our Unleash server. We are also setting the refresh and metrics intervals for our client.

We then create a new Unleash client instance using the “NewClient” method and start the client using the “Start” method.

we are using the Unleash client to check if the “new-feature” toggle is enabled. We are also providing a default value of “false” in case the feature toggle is not found.

We then use the “isEnabled” value to execute code conditionally. If the feature toggle is enabled, we execute code for the new feature, otherwise, we execute the default code.

To use this library, you need to import the “github.com/Unleash/unleash-client-go/v3” package and install it using the “go get” command:

go get github.com/Unleash/unleash-client-go/v3

finally, I hope this article helps you figure out how to use the feature toggle.

Ahmed Ghazey
Ahmed Ghazey

Written by Ahmed Ghazey

Seasoned Senior Staff Software Engineer with a proven track record of delivering complex software solutions, managing teams, and providing technical leadership.

No responses yet