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
Option | Type | Description |
---|---|---|
PublicKey | string | Your Hyphen API public key |
Application | string | The application id or alternate id |
Environment | string | The environment in which your application is running (e.g., production , staging ) |
EnableUsage | bool | Enable or disable the logging of toggle usage (telemetry) |
Cache | object | Configuration 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
},
},
}
Updated 8 days ago