Home
Softono
ff-golang-server-sdk

ff-golang-server-sdk

Open source Apache-2.0 Go
11
Stars
16
Forks
2
Issues
54
Watchers
4 months
Last Commit

About ff-golang-server-sdk

ff-golang-server-sdk is a Go SDK for Harness Feature Flags, enabling applications to integrate with the Harness feature flagging service. It supports both non-blocking and blocking initialization modes, allowing flexibility in how the client connects to the FF service. Developers can evaluate flags, retrieve variations, and receive real-time updates through streaming or a Relay Proxy. The SDK requires Golang 1.20 or later for version v0.1.21 and above, with earlier versions supporting Golang 1.6 through 1.19. Installation is done via go get. Key options include WithWaitForInitialized for blocking startup and WithMaxAuthRetries for controlling authentication attempts. Typical use cases include progressive feature rollout, A/B testing, feature toggling, and delivering personalized user experiences in Go-based backend services.

Platforms

Web Self-hosted

Languages

Go

Golang SDK For Harness Feature Flags

Go Report Card

Table of Contents

Intro
Requirements
Quickstart
Further Reading
Build Instructions

Intro

Use this README to get started with our Feature Flags (FF) SDK for Golang. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out. This sample doesn’t include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the Golang SDK Reference.

For a sample FF Golang SDK project, see our test Golang project.

FeatureFlags

Requirements

Install the SDK

Install the golang SDK using go

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

Initialize the SDK

Non-blocking Initialization (Default)

By default, when initializing the Harness Feature Flags client, the initialization process is non-blocking. This means that the client creation call will return immediately, allowing your application to continue its startup process without waiting for the client to be fully initialized. Here’s an example of creating a non-blocking client:

client, err := harness.NewCfClient(apiKey)

In this scenario, the client will initialize in the background, making it possible to use the client even if it hasn’t finished initializing. Be mindful that if you attempt to evaluate a feature flag before the client has fully initialized, it will return the default value provided in the evaluation call.

Blocking Initialization

In some cases, you may want your application to wait for the client to finish initializing before continuing. To achieve this, you can use the WithWaitForInitialized option, which will block until the client is fully initialized. Example usage:

client, err := harness.NewCfClient(sdkKey, harness.WithWaitForInitialized(true))

if err != nil {
log.ErrorF("could not connect to FF servers %s", err)
}

In this example, WaitForInitialized will block for up to 5 authentication attempts. If the client is not initialized within 5 authentication attempts, it will return an error.

This can be useful if you need to unblock after a certain time. NOTE: if you evaluate a feature flag in this state the default variation will be returned.

// Try to authenticate only 5 times before returning a result
client, err := harness.NewCfClient(sdkKey, harness.WithWaitForInitialized(true), harness.WithMaxAuthRetries(5))

if err != nil {
log.Fatalf("client did not initialize in time: %s", err)
}

Code Sample

The following is a complete code example that you can use to test the harnessappdemodarkmode Flag you created on the Harness Platform. When you run the code it will:

  • Connect to the FF service.
  • Report the value of the Flag every 10 seconds until the connection is closed. Every time the harnessappdemodarkmode Flag is toggled on or off on the Harness Platform, the updated value is reported.
  • Close the SDK.
package main

import (
    "log"
    "os"
    "time"

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

var (
    flagName string = getEnvOrDefault("FF_FLAG_NAME", "harnessappdemodarkmode")
    apiKey   string = getEnvOrDefault("FF_API_KEY", "changeme")
)

func main() {
    log.Println("Harness SDK Getting Started")

    // Create a feature flag client
    client, err := harness.NewCfClient(apiKey)
    if err != nil {
        log.Fatalf("could not connect to CF servers %s\n", err)
    }
    defer func() { client.Close() }()

    // Create a target (different targets can get different results based on rules)
    target := evaluation.Target{
        Identifier: "golangsdk",
        Name:       "GolangSDK",
        Attributes: &map[string]interface{}{"location": "emea"},
    }

    // Loop forever reporting the state of the flag
    for {
        resultBool, err := client.BoolVariation(flagName, &target, false)
        if err != nil {
            log.Fatal("failed to get evaluation: ", err)
        }
        log.Printf("Flag variation %v\n", resultBool)
        time.Sleep(10 * time.Second)
    }

}

func getEnvOrDefault(key, defaultStr string) string {
    value := os.Getenv(key)
    if value == "" {
        return defaultStr
    }
    return value
}

Running the example

export FF_API_KEY=<your key here>
go run examples/getting_started.go

Running the example with Docker

If you dont have the right version of golang installed locally, or don't want to install the dependencies you can use docker to quick get started

export FF_API_KEY=<your key here>
docker run -e FF_API_KEY=$FF_API_KEY -v $(pwd):/app -w /app golang:1.17 go run examples/getting_started.go

Additional Reading

For further examples and config options, see the Golang SDK Reference.

For more information about Feature Flags, see our Feature Flags documentation.


Harness is a feature management platform that helps teams to build better software and to test features quicker.


Code Cleanup (Beta)

The go sdk supports automated code cleanup. For more info see the docs