Skip to main content

How to use feature flags in Go

· 4 min read
Chavez Harris

Before feature flags, releasing a new feature would require taking your application offline to make changes, then redeploying it. In addition, if you find bugs after the release, you'll have to start the process from scratch. Despite being a traditional approach, this won't serve your users and your business well today. A feature flagging solution allows you to release and even roll back features without downtime with just a few clicks.

Feature flags in go cover

What are feature flags?

A feature flag is a boolean value that can be used as a wrapper to control what features or components are rendered to end users. It can be used in a conditional statement to display a feature if its value is true. Feature flagging is widely applicable and can be used in many programming languages and frameworks. Let's examine how to use it in a Golang application to toggle an API endpoint.

Pre-requisites

With that covered, let's turn our attention to the sample Go API and implement feature flagging.

Using the sample app

1. Clone the GitHub repository

2. Run the following commands to install the required packages and run the app:

go build
./feature-flags-in-go-sample

3. Sending a GET request to http://localhost:8001/jobs should return a list of jobs as shown in Postman below:

Screenshot of jobs api

As part of the demo, I will be controlling this endpoint with a feature flag. As a result, when the feature flag is switched on, the endpoint will return a list of jobs. In order to demonstrate this, I'm going to use ConfigCat's feature flag service.

ConfigCat is a feature flag and remote configuration service that allows developers to quickly and easily control the functionality of their applications. With ConfigCat, you can turn features on and off, alter their configurations, and roll out updates gradually to specific users or groups. Targeting is supported through attributes, percentage-based rollouts, and segmentation, allowing you to tailor the experience for your users. ConfigCat is available for all major programming languages and frameworks and can be accessed as a SaaS or self-hosted service.

Integrating with ConfigCat

1. To use ConfigCat, you'll need to sign up for a free account.

2. In the dashboard, create a feature flag with the following details:

Adding a feature flag

3. Install the ConfigCat's Go SDK, then add it to the import statement in main.go. With this SDK, the app will be able to access the ConfigCat dashboard and probe the status of the feature flag. In the following step, we'll import and use it.

go get github.com/configcat/go-sdk/v7
import (
"github.com/configcat/go-sdk/v7"
)

4. Create the client using your ConfigCat's SDK Key. It is advisable to keep this key private and not include it in your code base.

var client = configcat.NewClient("YOUR_CONFIGCAT_SDK_KEY")

5. Create a variable called isJobsEndpointEnabled to store the status of the feature flag:

isJobsEndpointEnabled := client.GetBoolValue("jobsendpoint", false, nil)

6. I'll use the variable above in an if statement to enable the endpoint if it is true:

if isJobsEndpointEnabled {
http.HandleFunc("/jobs", allJobs)
}

You can find the complete code for the main.go file here.

Live demo

1. Head over to the ConfigCat dashboard and turn off the feature flag.

Turning off the feature flag

2. Try sending a GET request to the /jobs API endpoint and it should now return an error:

Jobs API endpoint turned off

Final thoughts

You can simplify and enhance your feature deployment workflows by using feature flags. One advantage of controlling a feature by its flag allows you to toggle it on or off quickly throughout your application. There are many languages and frameworks that support feature flagging. When rolling out steady features, doing a canary deployment, and implementing A/B tests using feature flags is a must. The advantage of using ConfigCat is that you can control all your features from one place and narrow your feature releases to specific user segments. With their simple interface, you and your team can add feature flags to your apps using their supported SDKs.

If you need help with Go programming for your software projects, Toptal has a network of expert Golang developers. Check out their freelance Golang developers here. By using feature flags and getting the right expertise, you can make your development process smoother and get the most out of Go.

Stay updated

For more posts like this and other announcements, follow ConfigCat on Twitter, Facebook, LinkedIn, and GitHub.