How to use feature flags in Go
Tags: featuremanagement, featureflags, go, configcat
Published: 2023-03-21 22:48:20

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.

## 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

- A code editor (e.g. [Visual Studio Code](https://code.visualstudio.com/))
- [NodeJS](https://nodejs.org/en/) (version 16 or higher)
- [Go](https://go.dev/) version 1.19
- [Postman](https://www.postman.com/) - An API platform for building and using APIs
- Basic Go knowledge

With that covered, let's turn our attention to the [sample Go API](https://github.com/configcat-labs/feature-flags-in-go-sample) and implement feature flagging.

### Using the sample app

**1.** Clone the [GitHub repository](https://github.com/configcat-labs/feature-flags-in-go-sample)

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

```sh
go build
```

```sh
./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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuly6lu1i8lpmk81fsy4.png)

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](https://configcat.com). 

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](https://app.configcat.com/signup).

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

![Adding a feature flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnpmsveh2rip727j9u4c.png)

**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.

```sh
go get github.com/configcat/go-sdk/v7
```

```go
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.

```go
var client = configcat.NewClient("YOUR_CONFIGCAT_SDK_KEY")
```

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

```go
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:

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

You can find the complete code for the `main.go` file [here](https://github.com/configcat-labs/feature-flags-in-go-sample/blob/master/main.go).

## Live demo

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

![Turning off the feature flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n33y3jfzaslhi6oz8z0w.png)

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

![Jobs API endpoint turned off](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/040is67b0q87wwd4cdw5.png)
 
## 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](https://github.com/orgs/configcat-labs/repositories) 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](https://configcat.com/docs/sdk-reference/overview/).

## Stay updated

For more posts like this and other announcements, follow ConfigCat on [Twitter](https://twitter.com/configcat), [Facebook](https://www.facebook.com/configcat), [LinkedIn](https://www.linkedin.com/company/configcat/), and [GitHub](https://github.com/configcat).