GO

Hyphen Toggle Provider for Go is an OpenFeature provider implementation that enables seamless feature flag evaluation in Go applications. This provider integrates Hyphen's feature flagging system with the OpenFeature SDK, providing robust feature management with minimal setup.

Installation

go get github.com/hyphen/openfeature-provider-go
go get github.com/open-feature/go-sdk

Setup and Initialization

To integrate the Hyphen Toggle provider into your application, follow these steps:

  1. Configure the provider with your PublicKey, Application and Environment.
    You can specify the environment in one of two formats:

    • Alternate ID (e.g., "production", "staging") — the environment in which your application is running.
    • Project Environment ID (e.g., pevr_abc123) — useful for internal references.
  2. Register the provider with OpenFeature.

 provider, err := toggle.NewProvider(toggle.Config{
     PublicKey:   "your-public-key",
     Application: "your-app-name",
     Environment: "development", // or project environment ID
 })
  
openfeature.SetProvider(provider)
  1. Create an OpenFeature client for your application and configure the context needed for feature targeting evaluations, incorporating user or application context.
client := openfeature.NewClient("my-app")

ctx := openfeature.NewEvaluationContext(
    "user-123",
    map[string]interface{}{
        "email":      "[email protected]",
        "plan":       "premium",
        "age":        25,
        "country":    "US",
        "beta_user":  true,
    },
)

Usage

Evaluation Context Example

The evaluation context allows you to pass targeting information:

stringFlag, _ := client.StringValue(context.Background(), "my-string-flag", "default", ctx)
numberFlag, _ := client.NumberValue(context.Background(), "my-number-flag", 0, ctx)

log.Printf("String Flag: %s", stringFlag)
log.Printf("Number Flag: %f", numberFlag)

Configuration

Provider Options

OptionTypeRequiredDescription
PublicKeystringYesYour Hyphen API public key.
ApplicationstringYesThe application id or alternate id.
EnvironmentstringYesThe environment identifier for the Hyphen project (project environment ID or alternateId).
HorizonUrls[]stringNoHyphen Horizon URLs for fetching flags.
EnableUsageboolNoEnable/disable telemetry (default: true).
CacheobjectNoConfiguration for caching feature flag evaluations.

Caching

The provider supports caching of evaluation results:

PropertyTypeDefaultDescription
TTLnumber300Time-to-live in seconds for cached flag evaluations.
KeyGenFunction-Custom function to generate cache keys from evaluation context.

Example with cache configuration:

config := toggle.Config{
    PublicKey:   "your-public-key",
    Application: "your-app",
    Environment: "development",
    Cache: &toggle.CacheConfig{
        TTL: time.Minute * 5,
        KeyGen: func(ctx toggle.EvaluationContext) string {
            return fmt.Sprintf("%s-%s", ctx.TargetingKey, ctx.GetValue("plan"))
        },
    },
}

Example

package main

import (
    "context"
    "log"
    "github.com/open-feature/go-sdk/openfeature"
    "github.com/hyphen/openfeature-provider-go/pkg/toggle"
)

func main() {
    // Initialize the provider
    provider, err := toggle.NewProvider(toggle.Config{
        PublicKey:   "your-public-key",
        Application: "your-app-name",
        Environment: "development", // or project environment ID
    })
    if err != nil {
        log.Fatal(err)
    }

    // Set as global provider
    openfeature.SetProvider(provider)

    // Create a client
    client := openfeature.NewClient("my-app")

    // Create evaluation context
    ctx := openfeature.NewEvaluationContext(
        "targeting-key-user-123",
        map[string]interface{}{
            "email": "[email protected]",
            "plan":  "premium",
        },
    )

    // Evaluate different types of flags
    boolFlag, _ := client.BooleanValue(context.Background(), "my-bool-flag", false, ctx)
    stringFlag, _ := client.StringValue(context.Background(), "my-string-flag", "default", ctx)
    numberFlag, _ := client.NumberValue(context.Background(), "my-number-flag", 0, ctx)

    log.Printf("Bool Flag: %v", boolFlag)
    log.Printf("String Flag: %s", stringFlag)
    log.Printf("Number Flag: %f", numberFlag)
}