---
title: Example usage of the Go SDK
slug: feature-experimentation/docs/example-usage-go
docTags: 
createdAt: 2026-08-05T20:40:01.000Z
---

After installing the Go SDK, import the Optimizely library, get your project's datafile, and create a client. Use the client to evaluate flag rules like A/B tests and flag deliveries.

This example walks through the following three key steps:

1. Evaluate a flag with the key `product_sort` using the `Decide` method. This also sends a decision event to Optimizely to record that the user was exposed to the experiment.
2. Run code based on the flag result. The SDK evaluates your flag rules and determines which variation the user is in. You can either
   - Check the flag's enabled state and read a configuration variable (`sort_method`) to determine which experience the user gets.
   - Check the flag variation directly and run the corresponding control or treatment code.
3. Track a conversion event called `purchased` to measure the experiment's impact. The `TrackEvent` method ties the purchase back to the A/B test and sends it to Optimizely so it displays on your [Experiment Results page](docId:_K6n7h_MZ8b0xn6yWPrrq).

```go
import optly "github.com/optimizely/go-sdk" // for v2: "github.com/optimizely/go-sdk/v2"

optimizely_client, err := optly.Client("SDK_KEY_HERE")
if err != nil {
	// handle the err
}
// create a user and decide a flag rule (such as an A/B test) for them
user := optimizely_client.CreateUserContext("user123", map[string]interface{}{"logged_in": true})
decision := user.Decide("product_sort", []decide.OptimizelyDecideOptions{})

var variationKey string
if variationKey = decision.VariationKey; variationKey == "" {
  fmt.Printf("[decide] error: %v", decision.Reasons)
  return
}

// execute code based on flag enabled state
enabled := decision.Enabled

if enabled {
  // get flag variable values
  var value1 string
  decision.Variables.GetValue("sort_method", &value1)
  // or:
  value2 := decision.Variables.ToMap()["sort_method"].(string)
}

// or execute code based on flag variation:
if variationKey == "control" {
  // Execute code for control variation
} else if variationKey == "treatment" {
  // Execute code for treatment variation
}

// Track a user event
user.TrackEvent("purchased", nil)
```

For a more detailed and runnable code example, see the [Quickstart](docId\:GqD16Oyh9sH37TwarqEHo).
