Hello! In this tutorial we’ll show you how to add feature flags to your Go app, using Unleash and the official Unleash Go SDK. With Unleash, an open-source feature flag service, you can add feature flags to your application to release new features faster.
In this tutorial, we’ll get information about a country from the REST Countries API, and its GraphQL counterpart using Go. We’ll use feature flags to decide whether to call the REST or the GraphQL version of the API.
For this tutorial, you’ll need the following:

The Unleash Server is a Feature Flag Control Service, which manages your feature flags and lets you retrieve flag data. Unleash has a UI for creating and managing projects and feature flags. You can perform the same actions straight from your CLI or server-side app using the Unleash API.
Go is a back-end language, so there are special considerations to plan around when implementing feature flags.
Most importantly, you must:

For a complete list of architectural guidelines, including caching strategies, see our best practices for building and scaling feature flag systems.
In this section, we’ll install Unleash, run the instance locally, log in, and create a feature flag. If you prefer, you can use other tools instead of Unleash, but you’ll need to update the code accordingly.
Use Git to clone the Unleash repository and Docker to build and run it. Open a terminal window and run the following commands:
You will now have Unleash installed onto your machine and running in the background. You can access this instance in your web browser at http://localhost:4242.
Log in to the platform using these credentials:
Click New feature flag to create a new feature flag.

Call it graphql-api and enable it in the development environment.

Everything’s now set up on the Unleash side. Let’s go to the code now.
We’ll use the standard net/http package to make our HTTP requests and the Unleash SDK to connect to your local Unleash instance and retrieve your feature flag.
Open a new tab in your terminal, and create a new folder (outside of the unleash folder).
Install dependencies:
Next, let’s make sure our setup is working. Make a call to the REST API to retrieve information about a country using its country code. Create a file named main.go:
Run the code:
You should see Country: Norway, Capital: Oslo in your terminal.
The point of this tutorial is to mimic a real-world scenario where, based on a boolean feature flag, you would migrate from a REST API to a GraphQL one. So far, we’ve just used REST.
Let’s create a static feature flag, for now, just to test that we can call both versions successfully. Update main.go:
Run the code again:
You should see Hello GraphQL, followed by Country: Norway, Capital: Oslo in your terminal.
Now, let’s connect our project to Unleash so that you can toggle that feature flag at runtime. If you wanted to, you could also do a gradual rollout or use the flag for A/B testing.
You’ll need 2 things:
http://localhost:4242/api/ for your local version.With these 2, you can initialize your Unleash client as follows:
Now, let’s add our client to our project, grab the feature flag from Unleash, and update our conditional statement. Don’t forget to also update the config with your API key:
See additional use cases in our Backend SDK with Go documentation.
Now that we’ve connected our project to Unleash and grabbed our feature flag, we can verify that if you disable that flag in your development environment, you stop seeing the Hello GraphQL message and only get the country information from the REST API.
Note: An update to a feature flag may take 30 seconds to propagate.
All done! Now you know how to add feature flags with Unleash in Go. You’ve learned how to:
Feel free to have a look at our Go Examples page for more.
Thank you