Go SDK Reference

Updated 4 months ago by Archana Singh

This topic explains how to use the Harness Feature Flag 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.

Before You Begin

Prerequisites

  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.

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/wings-software/ff-client-sdk-go

Step 2: Import the Library

import harness "github.com/wings-software/ff-client-sdk-go/pkg/api"

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 := harness.NewClient(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").
Firstname("John").
Lastname("doe").
Email("johndoe@acme.com").
Country("USA").
Attribute("height", 160).
Build()

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 (
"context"
"fmt"
"log"
"time"

harness "github.com/wings-software/ff-client-sdk-go/pkg/client"
"github.com/wings-software/ff-client-sdk-go/pkg/dto"
)

const sdkKey = "00000000-1111-2222-3333-444444444444"

const featureFlagKey = "toggle"

func main() {

client, err := harness.NewCfClient(sdkKey,
harness.WithUrl("http://34.82.119.242/api/1.0/"),
harness.WithEventSourceUrl("http://34.82.119.242/api/1.0/stream/environments/%s"),
harness.WithStreamEnabled(true),
)
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").
Firstname("John").
Lastname("Doe").
Email("john@doe.com").
Country("USA").
Custom("height", 186).
Build()

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

go func() {
for {
select {
case <-ctx.Done():
return
default:
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)
cancel()
}


Please Provide Feedback