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

Quick Start

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",
        Environment: "development",
    })
    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(
        "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)
}

Advanced Usage

Evaluation Context

The evaluation context allows you to pass targeting information:

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

Caching Configuration

Configure caching to improve performance:

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"))
        },
    },
}

Configuration

Provider Options

OptionTypeDescription
PublicKeystringYour Hyphen API public key
ApplicationstringThe application id or alternate id
EnvironmentstringThe environment in which your application is running (e.g., production, staging)
EnableUsageboolEnable or disable the logging of toggle usage (telemetry)
CacheobjectConfiguration for caching feature flag evaluations

Caching

The provider supports caching of evaluation results:

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 ctx.TargetingKey
        },
    },
}