Skip to main content

Quickstart

The Eppo Go SDK enables feature flags and experiments in your Go applications with only a few lines of code.

The SDK handles all the complexity of feature flag evaluation and experiment assignment locally in your application, with no network calls required after initial setup. This guide will walk you through installing the SDK and implementing your first feature flag, experiment, and contextual bandit.

Installation

First, install the SDK using go get:

go get github.com/Eppo-exp/golang-sdk/v6

Feature Flags

Feature flags are a way to toggle features on and off without needing to deploy code.

Initialize the SDK

Create an SDK key if you don't already have one.

First, initialize the SDK using your SDK key:

import (
"github.com/eppo-exp/golang-sdk/v6/eppoclient"
)

var eppoClient *eppoclient.EppoClient

func main() {
config := eppoclient.Config{
SdkKey: "<SDK-KEY>",
}
client, err := eppoclient.InitClient(config)
if err != nil {
log.Fatal("Failed to initialize Eppo client:", err)
}

}
note

The SDK key is different from the project API key. You can find your SDK key in the SDK Keys section of the Eppo interface.

Assign a variant

Once initialized, you can start making assignments:

subjectAttributes := map[string]interface{}{
"country": user.Country,
"age": 30,
}

variation, err := client.GetStringAssignment(
"show-new-feature",
user.ID,
subjectAttributes,
"control",
)
if err != nil {
log.Printf("Error getting assignment: %v", err)
return
}

switch variation {
case "variant-a":
handleVariantA()
case "variant-b":
handleVariantB()
default:
handleControl()
}

Assignment Types

The SDK provides different assignment functions based on the type of value you need:

FunctionReturn Type
GetStringAssignment()string
GetBoolAssignment()bool
GetJSONAssignment()map[string]interface
GetIntegerAssignment()int64
GetNumericAssignment()float64
note

See more details about assignment functions in the Assignments page.

Experiments

While feature flags are useful, they do not send you any information about how your users are interacting with the feature. Experiments provide a way to collect data about these interactions using your preferred event logging system.

To log events through the SDK, you need to implement the AssignmentLogger interface:

type ConsoleLogger struct{}

func (l *ConsoleLogger) LogAssignment(event eppoclient.AssignmentEvent) error {
log.Printf("Assignment: %+v", event)
return nil
}

config := eppoclient.Config{
SdkKey: "<SDK-KEY>",
AssignmentLogger: &ConsoleLogger{},
client, err := eppoclient.InitClient(config)
if err != nil {
log.Fatal("Failed to initialize Eppo client:", err)
}
note

In a production application, you would want to replace the log.Printf with an actual logging system. We have documentation on how to set up logging with multiple popular data warehouses and logging systems in the Assignments page.

Contextual Bandits

Contextual Multi-Armed Bandits are a way to dynamically optimize assignments based on user context. A bandit balances exploration of new actions with exploitation of known successful actions to maximize a specified metric.

Bandit Setup

Setting up a bandit requires implementing both an assignment logger and a bandit logger:

import (
"github.com/eppo-exp/golang-sdk/v6/eppoclient"
)

type MyLogger struct{}

func (l *MyLogger) LogAssignment(assignment *eppo.Assignment) error {
log.Printf("Logging assignment: %+v", assignment)
return nil
}

func (l *MyLogger) LogBanditAction(banditAction *eppo.BanditAction) error {
log.Printf("Logging bandit action: %+v", banditAction)
return nil
}

config := eppoclient.Config{
SdkKey: "<SDK-KEY>",
}
client, err := eppoclient.InitClient(config)
if err != nil {
log.Fatal("Failed to initialize Eppo client:", err)
}

Query the bandit for actions

Instead of making simple assignments with a bandit, you query the bandit for actions:

attributes := eppo.ContextAttributes{
NumericAttributes: map[string]float64{
"age": float64(user.Age),
},
CategoricalAttributes: map[string]string{
"country": user.Country,
},
}

actions := map[string]eppo.ContextAttributes{
"nike": {
NumericAttributes: map[string]float64{
"brand_affinity": 2.3,
},
CategoricalAttributes: map[string]string{
"previously_purchased": "true",
},
},
"adidas": {
NumericAttributes: map[string]float64{
"brand_affinity": 0.2,
},
CategoricalAttributes: map[string]string{
"previously_purchased": "false",
},
},
}

result, err := client.GetBanditAction(
"shoe-bandit",
user.ID,
attributes,
actions,
"control",
)
if err != nil {
log.Printf("Error getting bandit action: %v", err)
return
}

if result.Action != nil {
showShoeAd(result.Action)
} else {
showDefaultAd()
}
note

For full steps to create a bandit including UI steps, see the bandit quickstart.

Next Steps

Now that you've seen how to make assignments with the Eppo Go SDK, we recommend familiarizing yourself with: