All docs
Feature flagsSDKsGocode

Go

Read flags from Go, with typed accessors and an explicit not-found error.

What you'll have

A Go client resolving flags, where a typo fails loudly instead of reading as off.

You'll need
  • An API key from API Keys

Flags in Go, on the server.

go get github.com/varia-bly/go-sdk

Client

cfg := variably.DefaultClientConfig()
cfg.APIKey = os.Getenv("VARIABLY_API_KEY")
cfg.BaseURL = os.Getenv("VARIABLY_BASE_URL")

client, err := variably.NewVariablyClient(cfg, variably.NewDefaultLogger(variably.LogLevelInfo))
if err != nil {
    log.Fatalf("variably: %v", err)
}
defer client.Close()

BaseURL is required. There is no default: a wrong host reads as "every flag off", which is indistinguishable from a real rollout.

Reading flags

user := variably.UserContext{UserID: "user-123", Country: "JP"}

enabled, err := client.EvaluateFlagBool(ctx, "checkoutRedesign", false, user)
layout,  err := client.EvaluateFlagString(ctx, "checkoutLayout", "control", user)
flags,   err := client.AllFlags(ctx, user)

Also available: EvaluateFlagInt, EvaluateFlagFloat, EvaluateFlagJSON, and the batch forms EvaluateFlags and EvaluateGates.

A missing flag is an error

The typed accessors return (value, error), and a flag the project does not define returns FlagNotFoundError rather than your default — so a typo cannot look like a flag that is switched off.

enabled, err := client.EvaluateFlagBool(ctx, "checkoutRedesign", false, user)
if err != nil {
    if variably.IsFlagNotFound(err) {
        log.Fatalf("flag not defined in Variably: %v", err)
    }

    return err
}

Check every key once at start-up rather than discovering a typo on a request path:

missing, err := client.ValidateFlags(ctx, []string{"checkoutRedesign", "newPricing"}, user)
if len(missing) > 0 {
    log.Fatalf("flags not defined in Variably: %v", missing)
}