On this page

Experiment Go SDK

Official documentation for Amplitude Experiment's server-side Go SDK implementation.

This documentation is split into two sections for remote and local evaluation:

Remote evaluation

Implements fetching variants for a user through remote evaluation.

Install

Install the Go Server SDK using go get.

bash
go get github.com/amplitude/experiment-go-server

Quick start

  1. Initialize the experiment client
  2. Fetch variants for the user
  3. Access a flag's variant
go
// (1) Initialize the local evaluation client with a server deployment key.
client := remote.Initialize("<DEPLOYMENT_KEY>", nil)

// (2) Fetch variants for a user
user := &experiment.User{
  UserId:   "user@company.com",
  DeviceId: "abcdefg",
  UserProperties: map[string]interface{}{
    "premium": true,
  },
}
variants, err := client.Fetch(user)
if err != nil {
  // Handle error
}

// (3) Access a flag's variant
variant := variants["<FLAG_KEY>"]
if variant.Value == "on" {
    // Flag is on
} else {
    // Flag is off
}

Initialize

Initialize the SDK client in your server on startup. The deployment key argument you pass to the apiKey parameter must reside within the same project that you are sending analytics events to.

go
func Initialize(apiKey string, config *Config) *Client
go
client := remote.Initialize("<DEPLOYMENT_KEY>", nil)

Configuration

Configure the SDK client upon initialization.

EU data center

If you're using Amplitude's EU data center, configure the ServerZone option on initialization.

Fetch

Fetches variants for a user and returns the results. The function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.

go
func (c *Client) Fetch(user *experiment.User) (map[string]experiment.Variant, error)
go
user := &experiment.User{
    UserId:   "user@company.com",
    DeviceId: "abcdefg",
    UserProperties: map[string]interface{}{
        "premium": true,
    },
}
variants, err := client.Fetch(user)
if err != nil {
    // Handle error
}

After fetching variants for a user, you may to access the variant for a specific flag.

go
variant := variants["<FLAG_KEY>"]
if variant.Value == "on" {
    // Flag is on
} else {
    // Flag is off
}

Fetch V2 with options

Fetches variants for a user with options and returns the results. The function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.

go
func (c *Client) FetchV2WithOptions(user *experiment.User, fetchOptions *experiment.FetchOptions) (map[string]experiment.Variant, error)
go
user := &experiment.User{
    UserId:   "user@company.com",
    DeviceId: "abcdefg",
    UserProperties: map[string]interface{}{
        "premium": true,
    },
}
variants, err := client.FetchV2WithOptions(user, &experiment.FetchOptions{
    TracksAssignment: true,
    TracksExposure:   true,
})
if err != nil {
    // Handle error
}

FetchOptions

Local evaluation

Implements evaluating variants for a user through local evaluation. If you plan to use local evaluation, you should understand the tradeoffs.

Install

Install the Go Server SDK using go get.

bash
go get github.com/amplitude/experiment-go-server

Quick start

  1. Initialize the local evaluation client.
  2. Start the local evaluation client.
  3. Evaluate a user.
go
// (1) Initialize the local evaluation client with a server deployment key.
client := local.Initialize("<DEPLOYMENT_KEY>", &local.Config{
 // (Recommended) Enable local evaluation cohort targeting.
 CohortSyncConfig: &local.CohortSyncConfig {
   ApiKey: "<API_KEY>",
   SecretKey: "<SECRET_KEY>"
 }
})

// (2) Start the local evaluation client.
err := client.Start()
if err != nil {
  panic(err)
}

  // (3) Evaluate a user.
user := &experiment.User{DeviceId: "abcdefg"}
variants, err := client.EvaluateV2(user, nil)
if err != nil {
  panic(err)
}

Initialize

Initializes a local evaluation client.

Server deployment key

You must initialize the local evaluation client with a server deployment key to get access to local evaluation flag configs.

go
func Initialize(apiKey string, config *Config) *Client

Flag streaming

Use the StreamUpdates configuration to push flag config updates to the SDK (default false), instead of polling every FlagConfigPollingInterval milliseconds. Typically, the time for SDK to receive the update after saving is less than one second. The SDK reverts to polling if streaming fails. Configure FlagConfigPollingInterval configuration to set how much time flag configs take to update after they are modified (default 30s), as well for fallback.

Configuration

Configure the SDK client upon initialization.

EU data center

If you're using Amplitude's EU data center, configure the ServerZone option on initialization.

Config

AssignmentConfig

ExposureConfig

CohortSyncConfig

Start

Start the local evaluation client, pre-fetching local evaluation mode flag configs for evaluation and starting the flag config poller at the configured interval.

go
func (c *Client) Start() error

You should await the result of Start() to ensure that flag configs are ready before calling Evaluate()

go
err := client.Start()
if err != nil {
    panic(err)
}

Evaluate

Executes the evaluation logic using the flags pre-fetched on Start(). You must give evaluate a user object argument. You can optionally pass an array of flag keys if you require only a specific subset of flag variants.

Exposure tracking

Set ExposureConfig to enable exposure tracking. Then, set TracksExposure to true in EvaluateOptions when calling EvaluateV2WithOptions().

go
func (c *Client) EvaluateV2(user *experiment.User, flagKeys []string) (map[string]experiment.Variant, error)
go
// The user to evaluate
user := &experiment.User{DeviceId: "abcdefg"}

// Evaluate all flag variants
allVariants, err := client.EvaluateV2(user, nil)
if err != nil {
    // Handle Error
}

// Evaluate a specific subset of flag variants
specificVariants, err := client.EvaluateV2(user, []string{
    "<FLAG_KEY_1>",
    "<FLAG_KEY_2>",
})

// Access a variant
variant := allVariants["<FLAG_KEY>"]
if variant.Value == "on" {
    // Flag is on
} else {
    // Flag is off
}

Evaluate V2 With Options

Executes the evaluation logic using the flags pre-fetched on Start(). You must give evaluate a user object argument. You can optionally pass an array of flag keys if you require only a specific subset of flag variants.

go
func (c *Client) EvaluateV2WithOptions(user *experiment.User, options *EvaluateOptions) (map[string]experiment.Variant, error)
go
user := &experiment.User{DeviceId: "abcdefg"}
variants, err := client.EvaluateV2WithOptions(user, &local.EvaluateOptions{
    FlagKeys: []string{"flag-key-1", "flag-key-2"},
    TracksExposure: true,
})
if err != nil {
    // Handle Error
}

EvaluateOptions

Local evaluation cohort targeting

Since version 1.7.0, the local evaluation SDK client supports downloading cohorts for local evaluation targeting. Configure the CohortSyncConfig option with the analytics ApiKey and SecretKey on initialization to enable this support.

go
client := local.Initialize("<DEPLOYMENT_KEY>", &local.Config{
  // (Recommended) Enable local evaluation cohort targeting.
  CohortSyncConfig: &local.CohortSyncConfig {
    ApiKey: "<API_KEY>",
    SecretKey: "<SECRET_KEY>"
  }
})

Custom logging

Control log verbosity with the LogLevel configuration, or implement the LoggerProvider interface to integrate your own logger.

Log levels

The SDK supports these log levels:

  • Verbose: Detailed logging for deep debugging
  • Debug: Logging for development and troubleshooting
  • Info: General informational messages
  • Warn: Warning messages
  • Error: Error messages (default)
  • Disable: No logging

Custom logger

Implement the LoggerProvider interface to integrate your logging solution:

go
import "github.com/amplitude/experiment-go-server/logger"

type MyCustomLogger struct {
    // Your logger implementation
}

func (l *MyCustomLogger) Verbose(message string, args ...interface{}) {
    // Implement verbose logging
}

func (l *MyCustomLogger) Debug(message string, args ...interface{}) {
    // Implement debug logging
}

func (l *MyCustomLogger) Info(message string, args ...interface{}) {
    // Implement info logging
}

func (l *MyCustomLogger) Warn(message string, args ...interface{}) {
    // Implement warn logging
}

func (l *MyCustomLogger) Error(message string, args ...interface{}) {
    // Implement error logging
}

// Initialize with custom logger
client := local.Initialize("<DEPLOYMENT_KEY>", &local.Config{
    LogLevel: logger.Debug,
    LoggerProvider: &MyCustomLogger{},
})

Backward compatibility

The Debug configuration field is still supported. When set to true, it overrides LogLevel to Debug.

Was this helpful?