Go SDK Reference

Updated 3 months ago by Archana Singh

This topic explains how to use the Harness Feature Flags (FF) SDK in your Go application. To learn more about using a sample Feature Flag SDK code with a Go application, see the Go SDK GitHub repository.

In this topic:

Before You Begin


  1. Create a feature flag in Harness Feature Flag. Feature flags wrap your code and allow you to manage the feature release in a controlled way. See Create a Feature Flag.
  2. Ensure that you have created your SDK Key. See Create an SDK Key.
  3. Download the Harness Feature Flag Go SDK.
  4. A Go application to test your feature flag. If you do not have your Go Application, you can download a sample application from the Go SDK GitHub repository.
    1. On the Go SDK GitHub page, click Code and then clone the sample application. For more information, see Cloning a repository.
    2. Import your project in an IDE such as IntelliJ or Eclipse.

Use Harness Feature Flags SDKs with Go Application

Perform the following steps to get started with using the Feature Flags SDK in your Go application:

Once you have your feature flag ready, integrate the flag with the Golang SDK code.

Step 1: Install the Golang Feature Flag SDK

go get github.com/harness/ff-golang-server-sdk

Step 2: Import the Library

 import harness "github.com/harness/ff-golang-server-sdk/client"

Step 3: Create a Client Instance

Create a client instance for setting the communication with the SDK. The SDK keys authorize your application to connect to the Harness feature Flag client using an SDK key.

client, err := harness.NewCfClient(sdkKey)

Step 4: Add Target to Your Feature Flag

Define the target for your feature flag. A feature flag is evaluated for a particular target.

target := dto.NewTargetBuilder("key").
Email("[email protected]").
Attribute("height", 160).

Step 5: Evaluate Feature Flag

Once you have initialized the Feature Flag client for a target, evaluate it for your feature flag. A feature flag is evaluated for a particular target.

showFeature, err := client.BoolVariation(featureFlagKey, &target, false)

Step 6: Verify Your Feature Flag

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created.

Toggle the flag on and off to verify if your application is getting updated.

Sample Code for Golang

Here is a sample code for integrating your feature flag with the Golang SDK:

package main

import (

harness "github.com/harness/ff-golang-server-sdk/client"

const sdkKey = "your SDK key"

const featureFlagKey = "toggle"

func main() {

client, err := harness.NewCfClient(sdkKey)
defer func() {
if err := client.Close(); err != nil {
log.Printf("error while closing client err: %v", err)

if err != nil {
log.Printf("could not connect to CF servers %v", err)

target := dto.NewTargetBuilder("john").
Email("[email protected]").
Custom("height", 186).

ctx, cancel := context.WithCancel(context.Background())

go func() {
for {
select {
case <-ctx.Done():
showFeature, err := client.BoolVariation(featureFlagKey, &target, false)

if err != nil {
fmt.Printf("Error getting value: %v", err)

fmt.Printf("KeyFeature flag '%s' is %t for this user\n", featureFlagKey, showFeature)
time.Sleep(10 * time.Second)

time.Sleep(5 * time.Minute)

Please Provide Feedback